From f788fe3b5718ca261082060232c09cc1539bef54 Mon Sep 17 00:00:00 2001 From: Aditya Halder <115464203+Fenirok@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:54:13 +0530 Subject: [PATCH] Create MetaBinarySearch.java --- .../searches/MetaBinarySearch.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/MetaBinarySearch.java diff --git a/src/main/java/com/thealgorithms/searches/MetaBinarySearch.java b/src/main/java/com/thealgorithms/searches/MetaBinarySearch.java new file mode 100644 index 000000000000..6762307373fc --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/MetaBinarySearch.java @@ -0,0 +1,38 @@ +package com.thealgorithms.searches; + +public class MetaBinarySearch { + + /** + * Perform Meta Binary Search on a sorted array. + * + * @param arr The sorted array to search. + * @param target The target element to search for. + * @return The index of the target element if found, -1 otherwise. + */ + public int metaBinarySearch(int[] arr, int target) { + int n = arr.length; + int left = 0; + int right = n - 1; + + // Calculate the number of bits required for the maximum index + int maxBits = (int) Math.ceil(Math.log(n) / Math.log(2)); + + // Iterate over the bit length + for (int i = maxBits - 1; i >= 0; i--) { + int mid = left + (1 << i); + + // Check if mid index is within bounds + if (mid < n && arr[mid] <= target) { + left = mid; // move to the right half if condition is true + } + } + + // Check if we have found the target + if (left < n && arr[left] == target) { + return left; + } + + // If target not found + return -1; + } +}