Dsa
Binary Search
Easy
Closest Temperature
You are given a sorted list of temperatures from different cities and you need to find the closest temperature to a given temperature.
Input Format:
- The first line contains an integer n (1 <= n <= 10000), the number of temperature records.
- The second line contains n space-separated float numbers representing the temperatures sorted in ascending order.
- The third line contains a float number target_temperature.
Output Format:
- Print the temperature from the array that is closest to the target temperature.
Example:
Input:
5
20.0 21.5 22.5 23.0 24.0
21.0
Output:
21.5
Key concepts
binary_searcharraysearching
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