Dsa
Binary Search
Easy
Closest Integer in Rotated Sorted Array
Given a rotated sorted array of integers and a target integer, write a function to find the closest integer to the target in the array. If two integers are equally close to the target, return the smaller integer. The array will contain unique elements. The function should read and print the result as specified below.
Input format:
- The first line contains an integer n (1 <= n <= 1000), the number of elements in the array.
- The second line contains n integers representing the rotated sorted array.
- The third line contains a single integer target.
Output format:
- A single integer representing the closest number to the target.
Example:
Input:
5
4 5 6 7 1
3
Output:
4
Key concepts
binary_searcharraysorting
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