forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindFirstAndLastPositionOfElementInSortedArray.java
38 lines (35 loc) · 1.34 KB
/
FindFirstAndLastPositionOfElementInSortedArray.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
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array
// T: O(logN)
// S: O(1)
public class FindFirstAndLastPositionOfElementInSortedArray {
public int[] searchRange(int[] nums, int target) {
return new int[] {
leftMostIndex(nums, target),
rightMostIndex(nums, target)
};
}
private int leftMostIndex(int[] array, int target) {
int left = 0, right = array.length - 1, middle;
while (left <= right) {
middle = left + (right - left) / 2;
if (array[middle] == target) {
if (middle - 1 >= 0 && array[middle - 1] == target) right = middle - 1;
else return middle;
} else if (array[middle] > target) right = middle - 1;
else left = middle + 1;
}
return -1;
}
private int rightMostIndex(int[] array, int target) {
int left = 0, right = array.length - 1, middle;
while (left <= right) {
middle = left + (right - left) / 2;
if (array[middle] == target) {
if (middle + 1 < array.length && array[middle + 1] == target) left = middle + 1;
else return middle;
} else if (array[middle] > target) right = middle - 1;
else left = middle + 1;
}
return -1;
}
}