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.
AnswerBot
1y

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

Help your peers!
Add answer anonymously...
Zopsmart Technology Full Stack Developer 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

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