forked from JFulgoni/Java-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumberIndex.java
More file actions
71 lines (60 loc) · 1.81 KB
/
Copy pathSingleNumberIndex.java
File metadata and controls
71 lines (60 loc) · 1.81 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
60
61
62
63
64
65
66
67
68
69
70
71
package john_test;
import java.util.Arrays;
public class SingleNumberIndex {
public SingleNumberIndex(){
}
/*
* This code returns the index of a singleton in an array of pairs
* Where the array is a sorted array of ints
* It is similar to SingleNumber.java, and can be modified to return the number
* Instead of just the index
*/
public int findSingleIndex(int[] myArray, int low, int high){
if(low == high){
System.out.println("Index is: " + low);
return low; //now we're done
}
int midpoint = low + (high - low)/2;
System.out.println(low + " " + high + " " + midpoint);
if(midpoint % 2 == 0){
//if the midpoint is an even index
//compare it to the number to it's right
if(myArray[midpoint] == myArray[midpoint + 1]){
//if they are the same
//take the right half
return 1 * findSingleIndex(myArray, midpoint + 2, high);
}
else{
//if they are different
//then take the left half
return 1 * findSingleIndex(myArray, low, midpoint);
}
}
else{
//if the midpoint is an odd index
//compare it to the one on it's left
if(myArray[midpoint - 1] == myArray[midpoint]){
//if they are the same
//then take the right half
return 1 * findSingleIndex(myArray, midpoint + 1, high);
}
else{
//if they are different
//take the left half
return 1 * findSingleIndex(myArray, low, midpoint - 1);
}
}
}
public void printArray(int [] array){
for(int i = 0; i < array.length; ++i){
System.out.print(array[i] + " ");
}
System.out.print("\n");
}
public static void main(String[] args) {
SingleNumberIndex s = new SingleNumberIndex();
int[] testArray = new int[]{3, 3, 4, 5, 5, 6, 6, 7, 7};
int result = s.findSingleIndex(testArray, 0, testArray.length - 1);
System.out.println("Result is: " + result);
}
}