-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowAndHighIndexApp.java
More file actions
38 lines (30 loc) · 937 Bytes
/
LowAndHighIndexApp.java
File metadata and controls
38 lines (30 loc) · 937 Bytes
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
package gr.aueb.cf.ch10.projects;
public class LowAndHighIndexApp {
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 4, 4, 4, 4};
int[] returned;
returned = getLowAndHighIndexOf(arr, 1);
System.out.printf("(%d, %d)", returned[0] + 1, returned[1] + 1);
}
public static int[] getLowAndHighIndexOf(int[] arr, int key) {
int low = 0;
int high = 0;
int pivot;
boolean found = false;
if (arr == null) return new int[] {};
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
low = i;
found = true;
break;
}
}
if (!found) return new int[] {};
high = low;
pivot = low + 1;
while ((pivot < arr.length) && (arr[pivot++] == key)) {
high++;
}
return new int[] {low, high};
}
}