easyBinary SearchPattern: Binary Search

Binary Search for Target Speed Solution

Problem Statement

You are given a sorted array of integers `laneSpeeds` and a target speed `targetSpeed`. Implement a function to find the index of the lane with the target speed using binary search.

Examples

Example 1:
Input:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270], 150
Output:14
Explanation: The lane with speed limit 150 is at index 14 in the sorted array
Example 2:
Input:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270], 10
Output:0
Explanation: The lane with speed limit 10 is at index 0 in the sorted array

Constraints

  • Each zone is a 2D array of integers.
  • Zones can have different sizes and their sizes are known.
Time: O(m log n) Space: O(1)
The optimized approach is to use a binary search algorithm, which has a time complexity of O(log n).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.