-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstAndLastPosition.java
More file actions
62 lines (43 loc) · 1.31 KB
/
Copy pathFirstAndLastPosition.java
File metadata and controls
62 lines (43 loc) · 1.31 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
package com.binarySearch;
import java.util.Arrays;
public class FirstAndLastPosition {
public static void main(String[] args) {
int [] nums = {5,7,7,8,8,10};
int target = 8;
System.out.println(Arrays.toString(searchRange(nums, target)));
}
static int[] searchRange(int[] nums, int target) {
int[] result = {-1, -1};
result[0] = search(target, nums, true);
if( result[0] != -1) {
result[1] = search(target, nums, false);
}
return result;
}
static int search(int target, int[] arr, boolean searchForFirstOccurence){
int start = 0;
int end = arr.length - 1;
int ans = -1 ;
// while start is not greater than end
while(start <= end){
// better way to find the mid
int mid = start + (end - start) / 2;
// target is greater than mid
if(target > arr[mid]){
start = mid + 1;
}
else if(target < arr[mid]){
end = mid - 1;
}
else{
ans = mid;
if(searchForFirstOccurence){
end = mid - 1;
}else{
start = mid + 1;
}
}
}
return ans;
}
}