Dsa
Binary Search
Easy
Find Closest Meal Price
You run a meal delivery service and want to find the closest meal price to a desired budget. You have a list of meal prices and a budget given. You need to determine the price of the meal that is closest to the budget without going over. If no meal is available below the budget, return -1.
Input:
- The first line contains the integer N (1 ≤ N ≤ 10^5), the number of meal prices.
- The second line contains N space-separated integers representing the meal prices.
- The third line contains the integer B (0 ≤ B ≤ 10^9), the budget.
Output:
- A single integer representing the closest meal price to the budget without exceeding it, or -1 if all prices exceed the budget.
Example:
Input:
5
12 15 8 20 10
14
Output:
12
Key concepts
binary_searchsortingarrays
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