forked from NiharRanjan53/HacktoberFest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_mountain.java
61 lines (59 loc) · 1.67 KB
/
target_mountain.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
59
60
61
package dsa;
import java.util.Arrays;
import java.util.Scanner;
public class target_mountain {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr={0,2,5,7,11,19,9,5,1};
System.out.println("enter the target");
int target=in.nextInt();
int[] ans={-1,-1};
ans[0]=position(arr,target,true);
ans[1]=position(arr,target,false);
System.out.println(Arrays.toString(ans));
}
static int position(int[] arr, int a,boolean i){
int start=0;int end=arr.length-1;
int p=peak(arr);
if(a==arr[p]){
return p;
}
if(i){
end=p-1;
while(start<=end){
int mid=(start+end)/2;
if (a<arr[mid]){
end=mid-1;
} else if (a>arr[mid]) {
start=mid+1;
} else if (a==arr[mid]) {
return mid;
}
}
}else{
start=p+1;
while(start<=end){
int mid=(start+end)/2;
if (a>arr[mid]){
end=mid-1;
} else if (a<arr[mid]) {
start=mid+1;
} else if (a==arr[mid]) {
return mid;
}
}
}return -1;
}
static int peak(int[] arr){
int start=0;
int end =arr.length-1;
while(start<end){
int mid=start+(end-start)/2;
if(arr[mid]<arr[mid+1]){
start=mid+1;
}if(arr[mid]>arr[mid+1]){
end=mid;
}
}return start;
}
}