Dsa
Binary Search
Hard
Closest Number Search
You are given a sorted list of integers representing temperatures recorded over several days. Given a target temperature, your task is to find the closest temperature from the list to the target. If there are two temperatures equally close to the target, return the smaller temperature. Your program should read the list of temperatures and the target temperature and print the closest temperature.
Input Format:
- The first line contains an integer n (1 <= n <= 100,000) representing the number of temperature readings.
- The next n lines contain integers representing the temperatures.
- The last line contains the target temperature.
Output Format:
- Print the closest temperature.
Example:
Input:
5
15
20
25
30
35
22
Output:
20
Key concepts
binary_searcharraysdifference
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