-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathque2.java
More file actions
57 lines (50 loc) · 1.19 KB
/
que2.java
File metadata and controls
57 lines (50 loc) · 1.19 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
package assignmentt;
public class que2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int[] searchRange(int[] nums, int target) {
int res[]=new int[2];
res[0]=first(nums,target);
res[1]=last(nums,target);
return res;
}
public int first(int nums[],int target){
int lo=0;
int hi=nums.length-1;
int ans=-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(nums[mid]==target){
ans=mid;
hi=mid-1;
}
else if(nums[mid]>target){
hi=mid-1;
}
else {
lo=mid+1;
}
}
return ans;
}
public int last(int nums[],int target){
int lo=0;
int hi=nums.length-1;
int ans=-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(nums[mid]==target){
ans=mid;
lo=mid+1;
}
else if(nums[mid]<target){
lo=mid+1;
}
else{
hi=mid-1;
}
}
return ans;
}
}