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.
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].
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].
Master coding challenges related to Arrays and solve the [Backup] Galaxy Anomaly Detection problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak