Dsa
Binary Search
Medium
Find Closest Value in Sorted Array
Given a sorted array of integers and a target integer, write a function that finds the element in the array that is closest to the target. In case of a tie, return the smaller value.
Input Format:
- The first line contains an integer n, the size of the array.
- The second line contains n integers, representing the sorted array.
- The third line contains the target integer.
Output Format:
- Print the closest value to the target.
Example:
Input:
5
1 3 5 7 9
6
Output:
5
Key concepts
binary_searcharrays
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