Binary Search | Practice | GeeksforGeeks

Given a sorted array of size N and an integer K, find the position at which K is present in the array using binary search.

Example 1:
Input:
N = 5
arr[] = {1 2 3 4 5} 
K = 4
Output: 3
Explanation: 4 appears at index 3.

Example 2:
Input:
N = 5
arr[] = {11 22 33 44 55} 
K = 445
Output: -1
Explanation: 445 is not present.

Your Task:  
You dont need to read input or print anything. Complete the function binarysearch() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1.

Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(LogN) if solving recursively and O(1) otherwise.

Constraints:
1 <= N <= 104
1 <= arr[i] <= 104
0
aggshubham12339
This comment was deleted.
0
aggshubham12339
This comment was deleted.
+2
abhishekniraj8022 days ago
in C++ solution
class Solution{public:   int binarysearch(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) high = mid - 1;           else low = mid + 1;                  }              return -1;   }};
0
koulikmaity3 days ago
int binarysearch(int arr[], int n, int k){       int s = 0;       int e = n-1;       int mid = s + (e - s) / 2;       while(s <= e)       {           if(arr[mid] == k)               return mid;                          if(arr[mid] <= k)           {               s = mid + 1 ;           }           if(arr[mid] > k)           {               e = mid - 1;           }           mid = s + (e - s) / 2;       }       return -1;   }
0
sidvas584 days ago
Why does this show time limit exceeded?
 
class Solution{public:   int binarysearch(int arr[], int n, int k){       // code here       int l=0,h=n-1;       int mid=l+(h-l)/2;       while(l<=h)       {           if(arr[mid]==k)           return mid;           else if(arr[mid]<k)           l=mid+1;           else           h=mid-1;       }       return -1;   }};
0
aggshubham12339
This comment was deleted.
0
priyansh708905 days ago
    int binarysearch(int arr[], int n, int k){       // code here       int start = 0;       int end = n-1;              int mid = start + (end-start)/2;              while(start<=end)       {           // if we find key            if(arr[mid] == k)           {               return mid;           }                      // if key is Less than mid                       else if(arr[mid] > k)           {               end = mid - 1;           }                      // if key is greater than mid           else           {               start = mid +1;           }           // find new mid            mid = start + (end-start)/2;       }       return -1;   }
-1
ashok1si18ec0175 days ago
class Solution {   int binarysearch(int arr[], int n, int k){       // code here       int count=-1;       for(int i=0;i<n;i++)       {           if(arr[i]==k)           {               count=i;           }       }       return count;   }}
0
mukuldhurkunde5 days ago
Python3
for i in range(len(arr)):
	if arr[i] == k:
	   return i
 return -1
0
pallemadhuyadhav29296 days ago
B
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.