Find Indices for Local Minima and Maxima
Given an integer array arr
of size N
, your task is to determine all indices of local minima and local maxima within the array. Return these indices in a 2-D list, where the first row denotes indices of local minima and the second row denotes indices of local maxima. If there are no local minima or maxima, return -1
as the only element in that row.
A local minimum is defined as an element that is smaller than its neighbors, and a local maximum is an element that is larger than its neighbors. For corner elements, consider only one neighbor for comparison.
Input:
The first line contains an integer ‘T’, representing the number of test cases.
For each test case:
The first line contains an integer N
denoting the size of the array.
The second line contains N
space-separated integers representing the array elements.
Output:
For each test case:
First line: Space-separated indices of local minima.
Second line: Space-separated indices of local maxima.
Example:
Input:
1
7
10 5 20 30 40 35 50
Output:
1 5
0 4 6
Explanation:
Indices 1 and 5 are local minima because those elements are smaller than their neighbors.
Indices 0, 4, and 6 are local maxima because they are greater than their neighbors.
Constraints:
1 <= T <= 10
2 <= N <= 10^5
-10^9 <= arr[i] <= 10^9
- Time Limit: 1 sec
Note:
You are not required to print anything, as it is already handled. Implement the function to return the correct result.
The task is to find and return the indices of local minima and local maxima in the given array.
Iterate through the array and compare each element with its neighbors to determine if it is a local minim...read more
Top Zopsmart Technology Full Stack Developer interview questions & answers
Popular interview questions of Full Stack Developer
Reviews
Interviews
Salaries
Users/Month