-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinimumDifferenceElement.java
40 lines (33 loc) · 1.17 KB
/
MinimumDifferenceElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package BinarySearch;
import Leetcode.MaximumFrequencyStack;
/**
* @author kalpak
*
* Given a sorted array, find the element in the array which has minimum difference with the given number.
*
*/
public class MinimumDifferenceElement {
public static int minimumDifference(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
if (nums[nums.length-1] <= target) return nums[nums.length-1];
while(left <= right) {
int mid = left + (right - left)/2;
if(nums[mid] == target)
return nums[mid];
else if (nums[mid] > target)
right = mid - 1;
else
left = mid + 1;
}
if (nums[left] - target < target-nums[right])
return nums[left];
return nums[right];
}
public static void main(String[] args) {
System.out.println(minimumDifference(new int[] { 4, 6, 10 }, 7));
System.out.println(minimumDifference(new int[] { 4, 6, 10 }, 4));
System.out.println(minimumDifference(new int[] { 1, 3, 8, 10, 15 }, 12));
System.out.println(minimumDifference(new int[] { 4, 6, 10 }, 17));
}
}