Skip to content

Commit c879dae

Browse files
authored
Merge pull request #854 from priyanka2710singh/master
Created a Mountain.java file
2 parents 6d901ca + 409130a commit c879dae

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

05. Searching/Mountain.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
public class Mountain
3+
{
4+
public static int peakIndexInMountainArray(int n, int[] arr)
5+
{
6+
int start =0;
7+
int end= arr.length-1;
8+
while(start<end)
9+
{
10+
int mid = start +(end - start) / 2;
11+
if(arr[mid]>arr[mid+1])
12+
{
13+
end=mid;
14+
}
15+
else{
16+
start = mid+1;
17+
}
18+
19+
}
20+
return start;
21+
}
22+
23+
public static void main(String[] args) {
24+
25+
Scanner sc = new Scanner(System.in);
26+
int n =sc.nextInt();
27+
28+
int[] arr = new int[n];
29+
for(int i=0;i<n;i++)
30+
{
31+
arr[i] =sc.nextInt();
32+
}
33+
int ans = peakIndexInMountainArray(n,arr);
34+
System.out.println(ans);
35+
36+
}
37+
}

0 commit comments

Comments
 (0)