
Minimum Difference Element (medium)
Problem Statement
Given an array of numbers sorted in ascending order, find the element in the array that has the minimum difference with the given key.
Example 1:
Input: [4, 6, 10], key = 7
Output: 6
Explanation: '6' is closer to the key '7' than any other number in the array.
Example 2:
Input: [4, 6, 10], key = 4
Output: 4
Example 3:
Input: [1, 3, 8, 10, 15], key = 12
Output: 10
Example 4:
Input: [4, 6, 10], key = 17
Output: 10
Constraints:
- 1 <= arr.length <= 10<sup>4</sup>
- -10<sup>4</sup> <= arr[i], key <= 10<sup>4</sup>
arris sorted in ascending order.- If two elements are equally close to the
key, return the smaller one. In[10, 20, 30, 40, 50]with key 35, both 30 and 40 are 5 away, and the answer is 30.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory