Page cover

Binary Search

Search Element using Binary search in the Linear & 2-D Arrays

int BinarySearch(int arr[], int key, int size){
    int s = 0; // starting index
    int e = size-1; // ending index 
    while(s <= e){
        int mid = s + (e - s)/2;
        if(arr[mid] == key){
            return mid;
        }

        // if arr[mid] != key, then decide the part using this conditions > or <
        else if(arr[mid] > key){
            e = mid - 1;
        }
        else{
            s = mid + 1;
        }
    }
    return -1;
} 

Q4: Pivot Index

Last updated

Was this helpful?