MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

ArrayPeak.java:1–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution
2{
3 // Function to find the peak element
4 // arr[]: input array
5 // n: size of array a[]
6 public int peakElement(int[] arr,int n)
7 {
8 //add code here.
9 int lo=0;
10 int hi=n-1;
11
12 while(lo<=hi)
13 {
14 int mid = lo + (hi-lo)/2;
15
16 if((mid == 0 || arr[mid]>=arr[mid-1]) && (mid==n-1 || arr[mid]>=arr[mid+1]))
17 {
18 return mid;
19 }
20
21 else if(mid>0 && arr[mid-1]>arr[mid])
22 {
23 hi=mid-1;
24 }
25
26 else
27 {
28 lo=mid+1;
29 }
30
31 }
32 return 0;
33 }
34}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected