From a0f3949ed05bef6e2204803ac5e9b5ac3566684e Mon Sep 17 00:00:00 2001 From: Komal Singh Date: Sun, 12 Oct 2025 21:20:10 +0530 Subject: [PATCH 1/3] Added Meta Binary Search algorithm --- Search/MetaBinarySearch.js | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Search/MetaBinarySearch.js diff --git a/Search/MetaBinarySearch.js b/Search/MetaBinarySearch.js new file mode 100644 index 0000000000..01c1723dc6 --- /dev/null +++ b/Search/MetaBinarySearch.js @@ -0,0 +1,40 @@ +/** + * Meta Binary Search (also known as One-Pass Binary Search) + * + * This algorithm uses bit manipulation to perform a binary search + * in a single pass without using recursion or a traditional loop-based binary search. + * + * It works on sorted arrays by progressively checking bits of the index + * and narrowing down the possible position of the target element. + * + * Time Complexity: O(log N) + * Space Complexity: O(1) + * + * @param {number[]} arr - A sorted array to search within. + * @param {number} target - The element to search for. + * @returns {number} - Index of the target if found, otherwise -1. + */ + +function metaBinarySearch(arr, target) { + let n = arr.length; + if (n === 0) return -1; + + // Get the highest power of 2 less than or equal to n + let pos = 0; + let bit = 1 << Math.floor(Math.log2(n)); + + while (bit > 0) { + let newPos = pos | bit; // test the bit + if (newPos < n && arr[newPos] <= target) { + pos = newPos; // move to that position + } + bit >>= 1; // move to the next bit + } + + return arr[pos] === target ? pos : -1; +} + +export { metaBinarySearch }; + +// Example usage: +// console.log(metaBinarySearch([1, 3, 5, 7, 9, 11], 7)); // Output: 3 From a84306c1522ab537c33439e2ae81ce217891b506 Mon Sep 17 00:00:00 2001 From: Komal Singh Date: Sun, 12 Oct 2025 21:29:16 +0530 Subject: [PATCH 2/3] Fixed code style issues for Meta Binary Search --- Search/MetaBinarySearch.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/Search/MetaBinarySearch.js b/Search/MetaBinarySearch.js index 01c1723dc6..44943fb4f0 100644 --- a/Search/MetaBinarySearch.js +++ b/Search/MetaBinarySearch.js @@ -1,40 +1,33 @@ /** * Meta Binary Search (also known as One-Pass Binary Search) * - * This algorithm uses bit manipulation to perform a binary search - * in a single pass without using recursion or a traditional loop-based binary search. - * - * It works on sorted arrays by progressively checking bits of the index - * and narrowing down the possible position of the target element. + * Reference: https://www.geeksforgeeks.org/meta-binary-search-one-pass-binary-search/ * + * Works on sorted arrays by using bit manipulation to perform binary search in a single pass. * Time Complexity: O(log N) * Space Complexity: O(1) * - * @param {number[]} arr - A sorted array to search within. + * @param {number[]} arr - A sorted array. * @param {number} target - The element to search for. * @returns {number} - Index of the target if found, otherwise -1. */ - -function metaBinarySearch(arr, target) { - let n = arr.length; +function MetaBinarySearch(arr, target) { + const n = arr.length; if (n === 0) return -1; - // Get the highest power of 2 less than or equal to n - let pos = 0; - let bit = 1 << Math.floor(Math.log2(n)); - - while (bit > 0) { - let newPos = pos | bit; // test the bit + let pos = -1; + for (let bit = Math.floor(Math.log2(n)); bit >= 0; bit--) { + const newPos = pos + (1 << bit); if (newPos < n && arr[newPos] <= target) { - pos = newPos; // move to that position + pos = newPos; } - bit >>= 1; // move to the next bit } return arr[pos] === target ? pos : -1; } -export { metaBinarySearch }; +export {MetaBinarySearch }; + // Example usage: // console.log(metaBinarySearch([1, 3, 5, 7, 9, 11], 7)); // Output: 3 From a8d53868a109203c995562740d405cf8e6f41112 Mon Sep 17 00:00:00 2001 From: Komal Singh Date: Sun, 12 Oct 2025 21:32:14 +0530 Subject: [PATCH 3/3] Fixed code style issues for Meta Binary Search with prettier --- Search/MetaBinarySearch.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Search/MetaBinarySearch.js b/Search/MetaBinarySearch.js index 44943fb4f0..abee4e746f 100644 --- a/Search/MetaBinarySearch.js +++ b/Search/MetaBinarySearch.js @@ -12,22 +12,21 @@ * @returns {number} - Index of the target if found, otherwise -1. */ function MetaBinarySearch(arr, target) { - const n = arr.length; - if (n === 0) return -1; + const n = arr.length + if (n === 0) return -1 - let pos = -1; + let pos = -1 for (let bit = Math.floor(Math.log2(n)); bit >= 0; bit--) { - const newPos = pos + (1 << bit); + const newPos = pos + (1 << bit) if (newPos < n && arr[newPos] <= target) { - pos = newPos; + pos = newPos } } - return arr[pos] === target ? pos : -1; + return arr[pos] === target ? pos : -1 } -export {MetaBinarySearch }; - +export { MetaBinarySearch } // Example usage: // console.log(metaBinarySearch([1, 3, 5, 7, 9, 11], 7)); // Output: 3