-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstandLast.java
More file actions
54 lines (41 loc) · 1.38 KB
/
FirstandLast.java
File metadata and controls
54 lines (41 loc) · 1.38 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
import java.sql.Array;
import java.util.Arrays;
public class FirstandLast{
public static void main(String[] args) {
int[] nums={};
int target=6;
// Output: [3,4]
int[] ans=searchRange(nums, target);
System.out.println(Arrays.toString(ans));
}
static int[] searchRange(int[] nums, int target) {
// check for first occurrence if target first
int start = search(nums, target, true);
int end = search(nums, target, false);
return new int[]{start,end};
}
static int search(int[] nums,int target,boolean findStartIndex ){
int ans = -1;
int start = 0;
int end = nums.length - 1;
while(start <= end) {
// find the middle element
// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java
int mid = start + (end - start) / 2;
if (target < nums[mid]) {
end = mid - 1;
} else if (target > nums[mid]) {
start = mid + 1;
} else {
// potential ans found
ans = mid;
if (findStartIndex) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return ans;
}
}