Search Insert Position

Learning 1D Array Binary Search DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Search Insert Position in a Sorted Array using Lower Bound and Binary Search, Binary Search, binary search, complexity analysis, pointer operations, array, Linked List Operations.

Problem Statement: You are given a sorted array arr of distinct values and a target value x. You need to search for the index of the target value in the array. If the value is present in the array, then return its index. Otherwise, determine the index where it would be inserted in the array while maintaining the sorted order. Input Specification: The input consists of a sorted array of distinct integers arr and an integer x. Output Specification: Return the 0 based index where x is found or where it would be inserted to maintain sorted order. Sample Test Cases: Example 1: Input: Output: Explanation: 6 is not present in the array. If 6 is inserted at index 3 (0 based indexing), the array will still be sorted: {1, 2, 4, 6, 7}. Example 2: Input: Output: Explanation: 2 is present in the array at index 1.