-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstandlastelementinSortedArray.java
More file actions
49 lines (49 loc) · 1.11 KB
/
Copy pathfirstandlastelementinSortedArray.java
File metadata and controls
49 lines (49 loc) · 1.11 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
public class firstandlastelementinSortedArray {
public int lowerBound(int arr[], int t, int n){
int s = 0;
int e = n-1;
int m = 0;
int f = -1;
while(s<=e){
m = s+(e-s)/2;
if(t == arr[m]){
f = m;
e = m-1;
}
else if(t<arr[m]){
e = m-1;
}
else{
s = m+1;
}
}
return f;
}
public int upperBound(int arr[], int t, int n){
int s = 0;
int e = n-1;
int m = 0;
int f = -1;
while(s<=e){
m = s+(e-s)/2;
if(t == arr[m]){
f = m;
s=m+1;
}
else if(t<arr[m]){
e = m-1;
}
else{
s = m+1;
}
}
return f;
}
public int[] searchRange(int[] nums, int t) {
int n = nums.length;
int ans[] = new int[2];
ans[0] = lowerBound(nums, t, n);
ans[1] = upperBound(nums, t, n);
return ans;
}
}