Code Logo

Marina Beacon Frequency Search

Published at05 Jan 2026
Easy 8 views
Like3

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

Example 1
Input
freqs = [10, 20, 35, 50], target = 30
Output
2
Explanation

35 is the first frequency >= 30, and its index is 2.

Example 2
Input
freqs = [10, 20, 35, 50], target = 60
Output
-1
Explanation

No frequency is >= 60.

Example 3
Input
freqs = [10,20,35,50], requests = [5,20,36]
Output
[10,20,50]
Explanation

The closest frequencies meeting each request are 10, 20, and 50.

Algorithm Flow

Recommendation Algorithm Flow for Marina Beacon Frequency Search
Recommendation Algorithm Flow for Marina Beacon Frequency Search

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.

let left = 0;
let right = freqs.length - 1;
let ans = -1;

while (left <= right) {
  let mid = Math.floor((left + right) / 2);
  if (freqs[mid] >= target) {
    ans = mid;
    right = mid - 1;
  } else {
    left = mid + 1;
  }
}
return ans;

This approach runs in O(log n) time, where n is the number of stored frequencies.

Best Answers

java
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;
    }
}