Meta binary search (also called one-sided binary search by Steven Skiena in The Algorithm Design Manual on page 134) is a modified form of binary search that incrementally constructs the index of the target value in the array. Like normal binary search, meta binary search takes O(log n) time.
Examples:
Input: [-10, -5, 4, 6, 8, 10, 11], key_to_search = 10 Output: 5 Input: [-2, 10, 100, 250, 32315], key_to_search = -2 Output: 0
The exact implementation varies, but the basic algorithm has two parts:
- Figure out how many bits are necessary to store the largest array index.
- Incrementally construct the index of the target value in the array by determining whether each bit in the index should be set to 1 or 0.
Approach:
- Store number of bits to represent the largest array index in variable lg
- Use lg to start off the search in a for loop
- If element is found return pos
- Otherwise incrementally construct index to reach the target value in the for loop
- If element found return pos otherwise -1
Below is the implementation of the above approach:
// C++ implementation of above approach #include <iostream> #include <vector> using namespace std; // Function to show the working of Meta binary search int bsearch(vector<int> A, int key_to_search) { int n = (int)A.size(); int lg = 0; // Set number of bits to represent largest array index while ((1 << lg) < n - 1) lg += 1; int pos = 0; for (int i = lg - 1; i >= 0; i--) { if (A[pos] == key_to_search) return pos; // Incrementally construct the // index of the target value int new_pos = pos | (1 << i); // find the element in one // direction and update position if ((new_pos < n) && (A[new_pos] <= key_to_search)) pos = new_pos; } // if element found return pos otherwise -1 return ((A[pos] == key_to_search) ? pos : -1); } // Driver code int main(void) { vector<int> A = { -2, 10, 100, 250, 32315 }; cout << bsearch(A, 10) << endl; return 0; } |
1
Reference: https://www.quora.com/What-is-meta-binary-search
Recommended Posts:
- Implementation of Wilson Primality test
- Lucky alive person in a circle | Set - 2
- Find the number of boxes to be removed
- Maximum removal from array when removal time >= waiting time
- Find maximum in a stack in O(1) time and O(1) extra space
- Queries to check whether a given digit is present in the given Range
- Find the top K items with the highest value
- Number of special pairs possible from the given two numbers
- Program to solve the Alligation Problem
- Find Largest Special Prime which is less than or equal to a given number
- Check if a number has digits in the given Order
- Program to print the Zigzag pattern
- multimap clear() function in C++ STL
- Samsung Semiconductor Institute of Research(SSIR Software) intern/FTE | Set-3
- Count all possible position that can be reached by Modified Knight
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


