Given an array A of N integers. The task is to find a peak element in it in O( log N ) .
An array element is peak if it is not smaller than its neighbours. For corner elements, we need to consider only one neighbour.
Note: There may be multiple peak element possible, in that case you may return any valid index (0 based indexing).
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N. Then in the next line are N space separated values of the array.
For each test case output will be 1 if the index returned by the function is an index with peak element.
You don't have to take any input. Just complete the provided function peakElement() and return the valid index.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
1 <= T <= 100
1 <= N <= 105
1 <= A <= 106
1 2 3
Testcase 1: In the given array, 3 is the peak element as it is greater than its neighbour.
Testcase 2: 4 is the peak element as it is greater than its neighbour elements.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.