forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPosition_last_first.java
More file actions
89 lines (78 loc) · 3.01 KB
/
FindPosition_last_first.java
File metadata and controls
89 lines (78 loc) · 3.01 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Time Complexity : O(log(N)) N is size of input
// Space Complexity : O(1) No Auxilary Array
// Did this code successfully run on Leetcode : YES
// Any problem you faced while coding this : How to get first and last index of target with binary search option
// edge case handling was tricky
// Your code here along with comments explaining your approach
class FindPosition_last_first {
private int firstPosition(int[] nums, int target){
int left = 0;
int right = nums.length -1;
while(left <= right){
int mid = left + (right-left)/2;
// if mid is target
if(nums[mid] == target){
// if mid == 0 or mid element is greater than previous element
// that means mid is the first position
if(mid == 0 || nums[mid] > nums[mid-1]){
return mid;
}else{
// else there are elementes to the left of current mid that are equal to target
right = mid - 1;
}
}
// search in lef side of array
else if(nums[mid] > target){
right = mid -1;
}
// search in right side of array
else {
left = mid + 1;
}
}
return -1;
}
private int lastPosition(int[] nums, int target){
int left = 0;
int right = nums.length -1;
while(left <= right){
int mid = left + (right-left)/2;
// if mid is target
if(nums[mid] == target){
// if mid == 0 or mid element is less than than next element
// that means mid is the last position
if(mid == nums.length -1 || nums[mid] < nums[mid+1]){
return mid;
}else{
// else there are elementes to the right of current mid that are equal to target
left = mid + 1;
}
}
// search in lef side of array
else if(nums[mid] > target){
right = mid -1;
}
// search in right side of array
else {
left = mid + 1;
}
}
return -1;
}
public int[] searchRange(int[] nums, int target) {
// edge case
if (nums == null || ( nums.length == 0) ) return new int []{-1,-1};
// find the first position of target in array using binary search
int first = firstPosition(nums,target);
// find the last posiiton of target in array using binay search
int last = lastPosition(nums,target);
return new int[]{first,last};
}
public static void main(String[] args) {
int []nums = {5,7,7,8,8,10};
int target = 8;
FindPosition_last_first obj = new FindPosition_last_first();
int [] arr =obj.searchRange(nums, target);
System.out.println(arr[0]+" "+arr[1]);
}
}