forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode34.java
More file actions
36 lines (33 loc) · 965 Bytes
/
Leetcode34.java
File metadata and controls
36 lines (33 loc) · 965 Bytes
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
package com.company;
public class Leetcode34 {
public static void main(String[] args) {
int[] arr={2,5,7,9,9,9,9,10,12,16};
int target=9;
int first=search(arr,target,true);
int last=search(arr,target,false);
System.out.println("First Occurence At="+first);
System.out.println("Last Occurence At="+last);
}
static int search(int[] arr, int target,boolean isStart){
int start=0,end=arr.length-1;int ans=-1;
while(end>=start){
int mid=start+(end-start)/2;
if(target>arr[mid]){
start=mid+1;
}
else if(target<arr[mid]){
end=mid-1;
}
else if(target==arr[mid]){
ans=mid;
if(isStart){
end=mid-1;
}
else{
start=mid+1;
}
}
}
return ans;
}
}