463. Minimum Absolute Difference to Target in Sorted Array

EasyBinary SearchArrayBinary Search

Given a sorted array nums and an integer target, return the minimum absolute difference between target and any element in nums. Solve in O(log n).

Input: A sorted integer array nums and integer target.

Output: Minimum value of |nums[i] - target| over all i.

Examples

Example 1
Input: [1,3,5,7,9], 4
Output: 1
Explanation: |3-4|=1 and |5-4|=1. Minimum is 1.
Example 2
Input: [1,3,5,7,9], 0
Output: 1
Explanation: |1-0|=1. Closest element is 1.
Example 3
Input: [2,4,6,8], 5
Output: 1
Explanation: |4-5|=1 or |6-5|=1. Min is 1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →