/**
* Problem link: https://practice.geeksforgeeks.org/problems/peak-element/1
*
* Problem statement:
*
* An element is called peakElementIndex peak element if its value is not smaller than the value of its adjacent elements(if they exists).
* Given an array arr[] of size N, find the index of any one of its peak elements.
* Note: The generated output will always be 1 if the index that you return is correct. Otherwise output will be 0.
*
* Expected Time Complexity: O(log N)
* Expected Auxiliary Space: O(1)
*/
#include
using namespace std;
class Solution {
public:
int getPeakElementIndex(int arr[], int n) {
// Get the peak element
int res = modifiedBinarySearch(arr, 0, n-1);
if (n == 1) {
return 0;
} else if (n == 2) {
if (arr[0] >= arr[1]) {
return 0;
} else {
return 1;
}
}
return res;
}
private:
int modifiedBinarySearch(int arr[], int l, int r) {
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle itself
if (mid == r) {
return r;
}
if (mid == 0) {
return 0;
}
if (arr[mid] >= arr[mid-1] && arr[mid] >= arr[mid+1]) {
return mid;
}
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid+1] > arr[mid]) {
return modifiedBinarySearch(arr, mid + 1, r);
}
// Else the element can only be present in right subarray
return modifiedBinarySearch(arr, l, mid - 1);
}
return 0;
}
};
/**
* Driver code starts here
*/
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int input[n];
for (int i = 0; i > input[i];
}
bool output = 0;
Solution sol;
int peakElementIndex = sol.getPeakElementIndex(input, n);
if (peakElementIndex = n)
cout = input[1])
output = 1;
else if (peakElementIndex == n-1 and input[n-1] >= input[n-2])
output = 1;
else if (input[peakElementIndex] >= input[peakElementIndex + 1] && input[peakElementIndex] >= input[peakElementIndex - 1])
output=1;
else
output=0;
cout