Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MetaBinarySearch.java #5955

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions MetaBinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class MetaBinarySearch {

// Function to perform Meta Binary Search
public static 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 (arr[left] == target) {
return left;
}

// If target not found
return -1;
}

// Main function to test the Meta Binary Search
public static void main(String[] args) {
int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
int target = 13;

int result = metaBinarySearch(sortedArray, target);
if (result != -1) {
System.out.println("Target element " + target + " found at index: " + result);
} else {
System.out.println("Target element " + target + " not found in the array.");
}
}
}