Peak Element Finder
For a given array of integers arr
, identify the peak element. A peak element is an element that is greater than its neighboring elements. Specifically, if arr[i]
is the peak, then both arr[i-1] < arr[i]
and arr[i+1] < arr[i]
.
Input:
First line contains an integer 'T', the number of test cases.
For each test case, first line contains an integer 'n', the number of elements.
Second line contains 'n' space-separated integers representing the array elements.
Output:
For each test case, return a single integer that is the peak element of the array.
Example:
Input:
2
5
1 2 3 2 1
5
7 8 9 6 4
Output:
3
9
Constraints:
1 ≤ T ≤ 50
1 ≤ n ≤ 105
-109 ≤ arr[i] ≤ 109
- There is always exactly one peak element in the array.
Note:
- Do not print anything; just return the peak element.
- The first element can be a peak if it's greater than the second element.
- The last element can be a peak if it's greater than the second last element.

AnswerBot
13d

Find the peak element in an array of integers.
Iterate through the array and check if the current element is greater than its neighbors.
Handle edge cases for the first and last elements of the array.
Re...read more

Help your peers!
Add answer anonymously...
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
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