Marina Beacon Frequency Search
You are given a sorted list of stored beacon frequencies and a single target frequency. Your task is to find the index of the smallest stored frequency that is greater than or equal to that target.
This is a standard lower bound search. If the target is found in the list, return its index. If it is not found, return the index of the first value larger than it. If all stored frequencies are smaller than the target, return -1.
The indices are 0-based.
For example, if freqs = [10,20,35,50] and target = 30, the answer is 2 (which points to 35). If target = 20, the answer is 1. If target = 60, the answer is -1.
Example Input & Output
35 is the first frequency >= 30, and its index is 2.
No frequency is >= 60.
The closest frequencies meeting each request are 10, 20, and 50.
Algorithm Flow

Solution Approach
Since the beacon frequencies are already sorted, we can use binary search to find the first index i such that freqs[i] >= target.
We maintain a search range [left, right]. In each step, we check the middle element. If it is greater than or equal to the target, it could be our answer, so we record it and search the left half. If it is smaller, we search the right half.
This approach runs in O(log n) time, where n is the number of stored frequencies.
Best Answers
class Solution {
public int marina_beacon_frequency_search(int[] freqs, int target) {
int low = 0, high = freqs.length - 1;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (freqs[mid] >= target) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
