


Assume that the first and the last element of the array is -∞.
You are given ‘arr’ = [4, 6, 3, 2], Here element 6 is greater than 4 as well as 3, the index of 6 is 1. Hence the answer is 1.
The first line of input contains a single integer ‘T’, representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the length of the ‘arr’ array.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array ‘arr’.
For each test case, return a single integer representing the index of the peak value of the element.
It is guaranteed that a solution will always be possible under the given constraints.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^6
2 <= |arr[i]| <= 10^8
arr[i] != arr[i + 1] for all valid i
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function.
In this approach, we will iterate through the whole array, and check for a possible solution, when the first index of the solution is found we will return it.
Algorithm:
In this approach, we will use binary search on the array. First, it doesn’t seem that binary search will work in an unsorted array but it does.
What we are essentially doing is going in the direction of the rising slope(by choosing the element which is greater than current). How does that guarantee the result? Think about it, there are 2 possibilities.
In 1. the answer is the end element because we take the boundary as -INFINITY
In 2. the answer is the largest element before the slope falls.
Algorithm: