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
koulikmaity4 hours 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
sidvas582 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
priyansh708902 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;   }
0
ashok1si18ec0172 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
mukuldhurkunde3 days ago
Python3
for i in range(len(arr)):
	if arr[i] == k:
	   return i
 return -1
0
pallemadhuyadhav29293 days ago
B
0
fenilvaghasiya2026
This comment was deleted.
+1
rahilarahman4 days ago
easy python code for binary search
 
class Solution: def binarysearch(self, arr, n, k): start=0       end=n-1       while start<=end :           mid=(start + end)//2           if arr[mid]==k:               return mid           elif  arr[mid]<k:               start=mid+1           else:               end=mid-1       return -1   
0
harshscode5 days ago
 int l=0;       int h=n-1;              while(l<=h)       {           int mid=(l+h)/2;           if(a[mid]==k)           return mid;           else if(a[mid]>k)           h=mid-1;           else           l=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.