forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstAndLastPositionInSortedArray.java
59 lines (51 loc) · 1.96 KB
/
FirstAndLastPositionInSortedArray.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
49
50
51
52
53
54
55
56
57
58
// Time Complexity : O(log n) as we are using binary search
// Space Complexity : O(1) Constant space as the amount of extra space used does not depend on number of elements
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Doing 2 binary searches to identify the first and the last index
class FirstAndLastPositionInSortedArray {
public int[] searchRange(int[] nums, int target) {
int first = findFirst(nums, target);
int last = findLast(nums, target);
return new int[]{first, last};
}
private int findFirst(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int firstIndex = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
firstIndex = mid;
high = mid - 1; // Continue to search in the left half
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return firstIndex;
}
private int findLast(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int lastIndex = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
lastIndex = mid;
low = mid + 1; // Continue to search in the right half
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return lastIndex;
}
public static void main(String[] args) {
FirstAndLastPositionInSortedArray finder = new FirstAndLastPositionInSortedArray();
int[] nums = {5, 7, 7, 8, 8, 10};
int target = 8;
int[] result = finder.searchRange(nums, target);
System.out.println("First and Last Position: [" + result[0] + ", " + result[1] + "]");
}
}