Dsa
Binary Search
Medium
Mountain Climbing Altitude Search
You are climbing a mountain and have recorded the altitudes at different checkpoints in a sorted list. Given a target altitude, write a program to find the closest altitude that is less than or equal to the target using binary search.
Input Format:
- The first line contains an integer m (1 <= m <= 1000), the number of checkpoints.
- The second line contains m space-separated integers representing the altitudes in sorted order.
- The third line contains an integer target representing the desired altitude.
Output Format:
- An integer representing the closest altitude less than or equal to target, or -1 if all altitudes are greater.
Example:
Input:
5
1000 1500 2000 2500 3000
1800
Output:
1500
Key concepts
binary_searcharraysearch
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now