-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNonDuplicateNumber.java
More file actions
61 lines (54 loc) · 1.85 KB
/
NonDuplicateNumber.java
File metadata and controls
61 lines (54 loc) · 1.85 KB
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
49
50
51
52
53
54
55
56
57
58
59
/**
* Created by sunmit9 on 02/04/17.
*
*
* https://leetcode.com/problems/single-element-in-a-sorted-array/
*
* Given a sorted array consisting of only integers where every element
* appears twice except for one element which appears once.
* Find this single element that appears only once.
*
* Input: [1,1,2,3,3,4,4,8,8]
* Output: 2
*
* Input: [3,3,7,7,10,11,11]
* Output 10
*
* Note: Your solution should run in O(log n) time and O(1) space.
*
*/
public class NonDuplicateNumber {
public static void main(String[] args) {
System.out.println("Non repeating number = " + singleNonDuplicate(new int[]{0,1,1}));
}
private static int singleNonDuplicate(int[] nums) {
return singleNonDuplicate(nums, 0, nums.length-1);
}
private static int singleNonDuplicate(int[] nums, int start, int end){
if(start == end){
return nums[start];
}
int center = (end-start)/2 + start;
if(center == 0){
return nums[start];
} else if(center % 2 == 0){
//if the center is even,
// if center number and next number are the same, the non duplicate number is on the right.
if(nums[center] == nums[center + 1]){
return singleNonDuplicate(nums, center + 1, end);
}else{
// else the number is on the left
return singleNonDuplicate(nums, start, center );
}
}else{
//if the center is odd,
// if center number and next number are the same, the non duplicate number is on the left.
if(nums[center] == nums[center + 1]){
return singleNonDuplicate(nums, start, center);
}else{
// else the number is on the right
return singleNonDuplicate(nums, center + 1, end);
}
}
}
}