BackmediumArraysAdobe

Local Maxima Indices Solution

Problem Statement

You are given an array of integers radiation_intensities. Identify all indices of local maxima where the intensity is greater than or equal to its neighboring values. For edge cases, consider the intensity as a local maxima if it is greater than or equal to its single neighboring value.

Example 1
Input
[1, 2, 3, 1, 2, 3, 4, 3, 2, 1]
Output
[3, 5]

Explanation: Step-by-step: with input [1, 2, 3, 1, 2, 3, 4, 3, 2, 1], we first check if the value at index 3 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (1) and next value (1), we include index 3 in the output. Then, we check if the value at index 5 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (3), we include index 5 in the output. However, this is incorrect because the problem statement says to consider the intensity as a local maxima if it is greater than or equal to its single neighboring value. Therefore, we should only include index 3 in the output.

Example 2
Input
[1, 3, 2, 5, 4, 3, 2, 1]
Output
[1, 3]

Explanation: Step-by-step: with input [1, 3, 2, 5, 4, 3, 2, 1], we first check if the value at index 1 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (1), we include index 1 in the output. Then, we check if the value at index 3 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (2) and next value (5), we include index 3 in the output.

Constraints

  • The input array will have a minimum of 1 element and a maximum of 1000 elements.
  • All elements in the array will be integers between 1 and 1000.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Local Maxima Indices — Problem Statement & Solution Guide

ArraysMediumPattern recognition and iteration
TimeO(n)
|
SpaceO(n)

Problem Description

You are given an array of integers radiation_intensities. Identify all indices of local maxima where the intensity is greater than or equal to its neighboring values. For edge cases, consider the intensity as a local maxima if it is greater than or equal to its single neighboring value.

Examples

Example 1

Input

[1, 2, 3, 1, 2, 3, 4, 3, 2, 1]

Output

[3, 5]

Explanation: Step-by-step: with input [1, 2, 3, 1, 2, 3, 4, 3, 2, 1], we first check if the value at index 3 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (1) and next value (1), we include index 3 in the output. Then, we check if the value at index 5 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (3), we include index 5 in the output. However, this is incorrect because the problem statement says to consider the intensity as a local maxima if it is greater than or equal to its single neighboring value. Therefore, we should only include index 3 in the output.

Example 2

Input

[1, 3, 2, 5, 4, 3, 2, 1]

Output

[1, 3]

Explanation: Step-by-step: with input [1, 3, 2, 5, 4, 3, 2, 1], we first check if the value at index 1 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (1), we include index 1 in the output. Then, we check if the value at index 3 is greater than or equal to its neighboring values. Since it is greater than or equal to its previous value (2) and next value (5), we include index 3 in the output.

Constraints

  • The input array will have a minimum of 1 element and a maximum of 1000 elements.
  • All elements in the array will be integers between 1 and 1000.

Optimal Approach & Strategy

A more efficient approach involves iterating through the array only once, comparing each element to its immediate neighbors to identify local maxima, resulting in a time complexity of O(n). This approach takes advantage of the fact that a local maxima must be greater than or equal to its neighboring values.

Brute Force Approach

A naive approach would involve using nested loops to compare each element to every other element in the array, resulting in a time complexity of O(n²). This approach is inefficient for large arrays. It would work by iterating over the array and for each element, checking all other elements to see if it's a local maxima.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function localMaximaIndices(radiation_intensities) { let result = []; for (let i = 0; i < radiation_intensities.length; i++) { if ((i === 0 || (radiation_intensities[i] >= radiation_intensities[i - 1] || radiation_intensities[i - 1] === 0)) && (i === radiation_intensities.length - 1 || radiation_intensities[i] >= radiation_intensities[i + 1])) { result.push(i); } } return result; }

Asked in Top Tech Interviews

Adobe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.