Skip to content

Commit 73fd6cd

Browse files
committed
Kunal Solution to find the peak element in a Mountain Array
1 parent 0b063ae commit 73fd6cd

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎src/BinaySearch/MountainArray.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,29 @@ else if (nums[middle] > nums[middle+1]){
5555
return peakIndex;
5656
}
5757

58+
/*
59+
Solution by Kunal Kushwaha
60+
*/
61+
static int peakIndexInMountainArray(int[] arr) {
62+
int startIndex = 0;
63+
int endIndex = arr.length -1;
64+
65+
while (startIndex < endIndex){
66+
67+
int middle = startIndex + (endIndex - startIndex)/2;
68+
69+
if (arr[middle] > arr[middle+1]){
70+
//You are in the Descending Part of the Array
71+
//This may be the answer but look at Left of the Array
72+
//This is why end!= mid -1;
73+
endIndex = middle;
74+
}else {
75+
//You are in the Ascending part of the Array
76+
startIndex = middle +1;
77+
}
78+
}
79+
80+
return startIndex;
81+
}
82+
5883
}

0 commit comments

Comments
 (0)