Searching an element in a sorted array | Practice | GeeksforGeeks

Given an array arr[] sorted in ascending order of size N and an integer K. Check if K is present in the array or not.

Example 1:
Input:
N = 5, K = 6
arr[] = {1,2,3,4,6}
Output: 1
Exlpanation: Since, 6 is present in 
the array at index 4 (0-based indexing),
output is 1.
 
Example 2:
Input:
N = 5, K = 2
arr[] = {1,3,4,5,6}
Output: -1
Exlpanation: Since, 2 is not present 
in the array, output is -1.

 
Your Task:
You don't need to read input or print anything. Complete the function searchInSorted() which takes the sorted array arr[], its size N and the element K as input parameters and returns 1 if K is present in the array, else it returns -1. 

Expected Time Complexity: O(Log N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 106
1 <= K <= 106
1 <= arr[i] <= 106
 
0
akasksingh0801 week ago
 static int searchInSorted(int arr[], int N, int K)
    {
        
        // Your code here
        int low =0; 
        int high=N-1;
        while(low<=high)
        {
            int mid = (low+high)/2;
            if(arr[mid]==K)
            return 1;
            
            if(arr[mid]>K)
            high=mid-1;
            
            else
            low=mid+1;
        }
        return -1;
    }
+1
kartikeyashokgautam2 weeks ago
JAVA Solution : -
 static int searchInSorted(int arr[], int N, int K)   {       int low = 0;       int high = N-1;       int res = -1;              while(low<=high)       {              int mid = low+(high-low)/2;                      if(K>arr[mid])           {               low = mid+1;           }                     else  if(K<arr[mid])           {                high=mid -1;           }                      else            {              return 1;           }                  }
        return -1;}
 
0
snehalgadge3 weeks ago
Java Solution 
 
class Solution{   static int searchInSorted(int arr[], int N, int K)   {               int lb = 0;        int ub = N-1;      while(lb<=ub){       int mid = lb+(ub-lb)/2;              if(arr[mid]==K){           return 1;       }       else if (arr[mid]>K){           ub = mid-1;       }       else{           lb = mid+1;       }   }   return -1;   }}
+2
mohammadtanveer75403 weeks ago
    int searchInSorted(int arr[], int N, int K) 
    { 
        int low=0, high=N-1,mid;
        while(high-low>1){
            mid=(low+high)/2;
            if(arr[mid]<K)
                low=mid+1;
            else
                high=mid;
        }
        if(arr[low]==K)
            return 1;
        else if(arr[high]==K)
            return 1;
        return -1;
       
    }
-1
amoghyelasangi4 weeks ago
ONLY SOME TEST CASES HAVE PASSED
def searchInSorted(self,arr, N, K):       for i in range (len(arr)):           if K in arr:               return arr[i]           else:               return -1
0
yoshis1 month ago
Solution in java (least time complexity)
class Solution{   static int searchInSorted(int arr[], int N, int K)   {       int start= 0;       int end= N-1;       // Your code here       int mid;       while(start<=end){       mid= start+ (end-start)/2;       if(arr[mid]==K){           return 1;       }else{           if(arr[mid]>K){               end=mid-1;           }else{               start=mid+1;           }       }            }     return -1; }   }
0
kumararchitkumar4831 month ago
CPP Solution
 
    int searchInSorted(int arr[], int N, int K)    {          int start=0,end=N-1;     int mid= start+(end-start)/2;            while(end>=start){   if(arr[mid]==K){       return 1;   }   else if(arr[mid]<K){       start=start+1;   }   else{       end=end-1;   }    mid= start+(end-start)/2;}          }};
0
amiransarimy1 month ago
Python Solutions
 
def searchInSorted(self,arr, N, K):
       low = 0
       high = N - 1
       
       while low <= high:
           mid = low +(high -low)//2
           if arr[mid] == K:
               return 1
           
           elif K < arr[mid]:
               high = mid -1
           
           else:
               low = mid + 1
       
       return -1
0
vishal8241151 month ago
212 cases passed java
 
class Solution{   static int searchInSorted(int arr[], int N, int K)   {             for(int i=0;i<N;i++){           if(arr[i]==K){               return 1;           }       }       return -1;            }}
0
0niharika22 months ago
int searchInSorted(int arr[], int N, int K) 
{ 
    int lo=0, hi=N-1;
    while(lo<=hi){
        int mid = (lo + hi)/2;      //dynamic
        if(arr[mid]==K)     return 1;
        if(K<arr[mid])      hi = mid - 1;
        if(K>arr[mid])      lo = mid + 1;
    }
    return -1;
}
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.