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.
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.
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Learning to learn Artificial Intelligence | An overview of Meta-Learning
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Best First Search (Informed Search)
- The Ubiquitous Binary Search | Set 1
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- A Problem in Many Binary Search Implementations
- Longest Common Prefix using Binary Search
- Finding minimum vertex cover size of a graph using binary search
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Binary Search
- Randomized Binary Search Algorithm
- Leaf nodes from Preorder of a Binary Search Tree
- C Program for Binary Search (Recursive and Iterative)
- Binary Search using pthread
- Find square root of number upto given precision using binary search
- Binary Search on Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.