sundeepblue
4/22/2014 - 3:06 PM

Find the number of occurrences of a given number in a sorted array

Find the number of occurrences of a given number in a sorted array

int get_lower_bound(int A[], int N, int target) {
    int low = 0, high = N-1;
    while(low <= high) {
        int mid = (low + high) >> 1;
        if(target <= A[mid])            // gist1
            high = mid-1;
        else
            low = mid+1;
    }
    return low;
}

int get_upper_bound(int A[], int N, int target) {
    int low = 0, high = N-1;
    while(low <= high) {
        int mid = low + (high - low) >> 1;
        if(target < A[mid])
            high = mid-1;
        else 
            low = mid+1;
    }
    return low;
}

int count_occurrence(int A[], int N) {
    
}