Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@
* [InterpolationSearch](Search/InterpolationSearch.js)
* [JumpSearch](Search/JumpSearch.js)
* [LinearSearch](Search/LinearSearch.js)
* [MetaBinarySearch](Search/MetaBinarySearch.js)
* [Minesweeper](Search/Minesweeper.js)
* [QuickSelectSearch](Search/QuickSelectSearch.js)
* [RabinKarp](Search/RabinKarp.js)
Expand All @@ -322,8 +323,8 @@
* [TernarySearch](Search/TernarySearch.js)
* [UnionFind](Search/UnionFind.js)
* **Sliding-Windows**
* [MaxSumSubarrayFixed](Sliding-Windows/MaxSumSubarrayFixed.js)
* [LongestSubarrayWithSumAtMost](Sliding-Windows/LongestSubarrayWithSumAtMost.js)
* [MaxSumSubarrayFixed](Sliding-Windows/MaxSumSubarrayFixed.js)
* **Sorts**
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
* [BeadSort](Sorts/BeadSort.js)
Expand Down
28 changes: 28 additions & 0 deletions Search/MetaBinarySearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Meta Binary Search
* https://www.geeksforgeeks.org/meta-binary-search-one-sided-binary-search/
*
* Builds the index bit by bit from the most-significant bit to the least-significant bit.
* Works on sorted arrays and returns the index of target, or -1 if not found.
*/

function metaBinarySearch(sortedArray, target) {
if (!Array.isArray(sortedArray) || sortedArray.length === 0) {
return -1
}

const n = sortedArray.length
const maxBit = Math.floor(Math.log2(Math.max(1, n - 1)))

let position = 0
for (let bit = maxBit; bit >= 0; bit--) {
const candidate = position + 2 ** bit
if (candidate < n && sortedArray[candidate] <= target) {
position = candidate
}
}

return sortedArray[position] === target ? position : -1
}

export { metaBinarySearch }
25 changes: 25 additions & 0 deletions Search/test/MetaBinarySearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { metaBinarySearch } from '../MetaBinarySearch'

describe('Meta Binary Search', () => {
test('returns index for found numeric element', () => {
expect(metaBinarySearch([2, 5, 8, 12, 16, 23, 38], 23)).toBe(5)
})

test('returns -1 when numeric element is missing', () => {
expect(metaBinarySearch([2, 5, 8, 12, 16, 23, 38], 9)).toBe(-1)
})

test('works with sorted strings', () => {
expect(metaBinarySearch(['alpha', 'beta', 'delta', 'omega'], 'delta')).toBe(
2
)
})

Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests cover empty input and typical cases, but don’t cover the n === 1 case (which currently produces maxBit = -Infinity). Adding a single-element array test would lock in expected behavior and guard against regressions if the loop logic changes.

Suggested change
test('returns index for a matching element in a single-element array', () => {
expect(metaBinarySearch([7], 7)).toBe(0)
})

Copilot uses AI. Check for mistakes.
test('returns -1 on empty input', () => {
expect(metaBinarySearch([], 1)).toBe(-1)
})

test('returns index for a matching element in a single-element array', () => {
expect(metaBinarySearch([7], 7)).toBe(0)
})
})
Loading