Dsa
Binary Search
Warm-up
Bookshelf Height Calculator
You are given a sorted list of bookshelf heights in centimeters. Your task is to find the maximum height that is less than or equal to a specified height using binary search. If all bookshelves are taller than the specified height, return -1.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 500) representing the number of bookshelves.
- The second line contains n space-separated integers representing the heights of the bookshelves.
- The third line contains an integer representing the target height.
Output format:
- Print the maximum bookshelf height that is less than or equal to the target height, or -1 if none exist.
Example:
Input:
5
150 160 170 180 190
165
Output:
160
Key concepts
binary_searcharrayheight
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