Asked inGroupon,SDE-2
Find Peak Element

Given an array of ‘n’ integers arr. Find the Peak element of the array. The peak element of an array is defined as that element which is greater than both of its neighbours. I.e if arr[i] is the peak element, arr[i-1]<arr[i] and arr[i+1]<arr[i].

It is guaranteed that there exists only one peak element in the array.
Note:
1.Do not print anything, just return the value of peak element of the array.
2.The first element can be the peak element if and only if the array is non-increasing i.e. it will be a peak if its equal to second element.
3.The last element can be the peak element if and only if the array is non decreasing i.e. it will be a peak if it's equal to second last element.
4.Consider 0 based Indexing.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.

The first line of each test case contains an integer ‘n’ denoting the number of elements in the given sequence. 


The second line of each test case contains ‘n’ space-separated integers denoting the elements in the sequence.
Output Format
For each test case, return a single integer denoting the peak element of the array
Constraints:
1 <= T <= 50
1 <= N <= 10^5
-10^9 <= arr[i] <= 10^9

Where ‘T’ is the total number of test cases, ‘N’ denotes the number of elements in the sequence and arr[i] denotes the ‘i-th’ element of the sequence.

Time limit: 1 second
Sample Input 1:
2
5
1 2 3 2 1
5
7 8 9 6 4
Sample Output 1:
  3
  9
Explanation of sample input 1 :
Test Case 1:
In the given array we can see that the peak element is 3 because the element to its left and right is 2 which is less than 3. If we take any other element lets say the element at index 1 which is 2 (0-based indexing) we see the element to its left is 1 which is less but the element to its right is 3 which is not less therefore 2 can not be a peak element of the array.

Test Case 2:
In the given array we can see that the peak element is 9 because the element to its left is 8 which is less than 9. Also, the element to its right is 6 which is less than 9.If we take any other element lets say the element at index 1 which is 8 (0-based indexing) we see the element to its left is 7 which is less but the element to its right is 9 which is not less therefore 8 can not be a peak element of the array.
Sample Input 2:
2
5
2 3 4 1 -4
6
1 2 3 4 5 6
Sample Output 2:
4
6
Be the first one to answer
Add answer anonymously...
Groupon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter