-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindFirstAndLastInSortedArray.java
48 lines (45 loc) · 1.49 KB
/
FindFirstAndLastInSortedArray.java
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
import java.util.Arrays;
/*
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
*/
public class FindFirstAndLastInSortedArray {
public static void main(String[] args) {
int[] nums={5,7,7,8,8,10};
int target=8;
int[] result=searchRange(nums,target);
System.out.println(Arrays.toString(result));
}
public static int[] searchRange(int[] nums, int target) {
int[] ans = {-1 , -1};
ans[0] = search(nums , target , true);
ans[1] = search(nums, target , false);
return ans;
}
public static int search(int[] nums, int target, boolean IsStarting) {
int ans = -1;
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] > target) {
end = mid - 1;
} else if (nums[mid] < target) {
start = mid + 1;
}else {
// possible ans found
ans = mid;
if (IsStarting) {
end = mid - 1;
}else {
start = mid + 1;
}
}
}
return ans;
}
}