BackmediumArrays PayPal PhonePe

Galaxy Anomaly Detection Solution

Problem Statement

Given an array of energy readings, find all indices of space anomalies, where a space anomaly is detected when the energy reading is greater than or equal to its neighboring readings.

Example 1
Input
[1, 0, 1, 0, 1]
Output
[0, 2, 4]

Explanation: Step-by-step: with input [1, 0, 1, 0, 1], we compare each element with its neighbors. For the first element 1, it is greater than its neighbor 0, so its index 0 is included in the output. The second element 0 is not greater than its neighbor 1, so its index 1 is not included. Similarly, the third element 1 is greater than its neighbor 0, so its index 2 is included. The fourth element 0 is not greater than its neighbor 1, so its index 3 is not included. The fifth element 1 is greater than its neighbor 0 (considering the edge case where the last element is compared with its previous neighbor), so its index 4 is included. Hence, the output is [0, 2, 4].

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

Explanation: Step-by-step: with input [5, 4, 3, 2, 1], we compare each element with its neighbors. For the first element 5, it is greater than its neighbor 4, so its index 0 is included in the output. The rest of the elements are not greater than their neighbors, so their indices are not included. Hence, the output is [0].

Constraints

  • 1 <= length of array <= 10^5
  • -10^5 <= each energy reading <= 10^5
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free