-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleElementInSortedArray.java
48 lines (43 loc) · 1.42 KB
/
SingleElementInSortedArray.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
41
42
43
44
45
46
47
48
class SingleElementInSortedArray {
public static void main(String[] args) {
int[] nums = {1,1,2,2,3,5,5};
int ans = singleNonDuplicateBest(nums);
System.out.println("The first occurence are: "
+ ans);
}
public static int singleNonDuplicateBest(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (mid % 2 == 1) {
mid--;
}
if (nums[mid] != nums[mid + 1]) {
right = mid;
} else {
left = mid + 2;
}
}
return nums[left];
}
public static int singleNonDuplicate(int[] nums) {
int low = 0, high = nums.length - 1, mid;
while (low < high) {
mid = (low + high) / 2;
if(mid % 2 == 0){ //even -> starting new number
if(nums[mid] == nums[mid - 1]){ //wrong case -> left missing
high=mid-2;
}else{ //left correct -> search in right
low=mid;
}
}else{ // odd position -previous number
if(nums[mid] == nums[mid - 1]){ //
low=mid+1; //correct left - moving to right
}else{
high=mid-1; //wrong case - left missing.
}
}
}
return nums[low];
}
}