The question is as follows:
Suppose you are given a very large integer array A with 1,000,000,000 elements, with elements sorted in descending order. However, only the first n elements contain data which are positive integers, but the value of n is unknown. The rest of the array elements contain zeroes.
Give the algorithm for a method search(A,k) to search for a key k in the array A. It returns the index of the array where k is found, or -1 otherwise. Your algorithm must run in worst case O(logn) time.
You may use binarySearch(A, k, left, right) that searches for k in A[left] through A[right] (assuming left to right is sorted in descending order).
The approaches that I have thought of so far:
Use a for loop to iterate from index 0 till the index containing the first 0 and compare against k. This takes O(n) time so doesn't fit the time limit.
Binary search on A itself. This takes O(log 1,000,000,000) time and exceeds the time limit.
I am kind of stuck here and unable to think of any other approaches.
What would be an approach that runs in worst case O(logn)?