Search insert position of K in a sorted array | Practice | GeeksforGeeks

Given a sorted array Arr[](0-index based) consisting of N distinct integers and an integer k, the task is to find the index of k, if its present in the array Arr[]. Otherwise, find the index where k must be inserted to keep the array sorted.

Example 1:
Input:
N = 4
Arr = {1, 3, 5, 6}
k = 5
Output: 2
Explaination: Since 5 is found at index 2 
as Arr[2] = 5, the output is 2.

Example 2:
Input:
N = 4
Arr = {1, 3, 5, 6}
k = 2
Output: 1
Explaination: Since 2 is not present in 
the array but can be inserted at index 1 
to make the array sorted.


Your Task:
You don't need to read input or print anything. Your task is to complete the function searchInsertK() which takes the array Arr[] and its size N and k as input parameters and returns the index.

Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 104
-103 ≤ Arr[i] ≤ 103
-103 ≤ k ≤ 103
0
sunnysharawat100023 minutes ago
 for(int i = 0;i < N; i++)
       {
           if(Arr[i] == k)
           {
               return i;
           }
           else 
           {
               int j;
               for(int i = 0 ; i < N ; i++)
               {
                   if(Arr[i] < k)
                   {
                       j = i;
                   }
                   else if(Arr[0] > k)
                   {
                       return 0;
                   }
               }
               return j+1;
           }
       }


       
 
 
+1
juyal_sid3 hours ago
//C++ One Liner | Fast and Efficient Solutio | 
return lower_bound(Arr.begin(),Arr.end(),k)-Arr.begin();
0
abhishek2061a1 day ago
Simple C++ Solution using Binary Search
 
int searchInsertK(vector<int>Arr, int N, int k)
    {
        // code here
        int low = 0, high = N-1;
        
        while(low <= high)
        {
            int mid = low + (high-low)/2;
            
            if(Arr[mid] == k)
            return mid;
            
            else if(Arr[mid] < k)
            low = mid+1;
            
            else
            high = mid-1;
        }
        
        return low;
    }
0
2019sushilkumarkori2 days ago
class Solution{
    public:
    int searchInsertK(vector<int>Arr, int N, int k)
    {
        // code here
        int low = 0;
        int high = N-1;
        while(low<=high){
            int mid = (low+high)/2;
            if(Arr[mid] == k){
                return mid;
            }
            else if(Arr[mid]>k){
                high = mid-1;
            }
            else{
                low = mid+1;
            }
        }
        return high+1;
    }
};
0
prabathsaisumanth2 days ago
JAVA Solution(0.39/3.09)
class Solution
{
    static int searchInsertK(int arr[], int N, int k)
    {
        // code here
        for(int i=0;i<N-1;i++)
        {
            if(arr[i]<k&&arr[i+1]>=k)
            {
                return i+1;
            }
            else if(arr[N-2]<k&&arr[N-1]<k)
            {
                return N;
            }
        }
        return 0;
    }
}
0
rohankundu8593 days ago
//C++ Solution 
 
  int searchInsertK(vector<int>Arr, int N, int k)   {       // Searching       bool isfound=false;       int lb=0;       int ub=N-1;       int mid=(lb+ub)/2;       while(lb<=ub)       {           if(Arr[mid]==k)           {               isfound=true;               break;           }           else if(Arr[mid]>k)           {               ub=mid-1;           }           else if(Arr[mid]<k)           {               lb=mid+1;           }           mid=(lb+ub)/2;       }       if(isfound)       {           return mid;       }       else       {           //inserting           int count=0;           bool i=false;           for(auto it=Arr.begin();it!=Arr.end();it++)           {               count++;               if(k<(*it))               {                   Arr.insert(it,k);                   i=true;                   break;               }           }           if(i==false)           {               Arr.push_back(k);               count++;           }           return (count-1);       }   }
0
debajyoti dev3 days ago
O(logN)
		int left=0, right=N-1;
		
        while(left<=right){
            int mid=(left+right)/2;
            if(Arr[mid]>k) right=mid-1;
            else if(Arr[mid]<k) left=mid+1;
            else return mid;
        }
        return left ;
 
0
ps253723 days ago
Easy C++ Solution just check if the element in the array is greater than or equal to the given k
Now return the index wherever you find the element greater than the given k
int searchInsertK(vector<int>Arr, int N, int k)   {       // code here       for(int i=0; i<N; i++){           if(Arr[i] >= k){               return i;           }       }   }
0
sarthakpandey20023 days ago
EASY C++ SOLUTION
int ans=0;   int searchInsertK(vector<int>Arr, int N, int k)   {       // code here       for(int i=0;i<N;i++){           if(Arr[i]<k)           ans=i+1;           else if(Arr[i]==k)           ans=i;       }       return ans;   }
-1
sarthakjagetiya10013 days ago
Total Time Taken: 0.03/2.2
C++
int searchInsertK(vector<int>Arr, int N, int k)
    {
        for(int i=0; i<N; i++){
            if(Arr[i]==k || Arr[i]>k){
                return i;
            }
        }
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.