Peak Temperature Indices ā Problem Statement & Solution Guide
Problem Description
Given a sequence of temperature values stored in the array temperatures, find the indices of all temperature readings that exceed their immediate neighbors. If no such readings exist, return an empty list.
Examples
Input
[10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12]
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Explanation: Step-by-step: Given the input [10, 20, 12, 15, 18, 12, 15, 18, 12, 15, 18, 12], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. If it does, we append its index to the list. Finally, we return the list. The output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] indicates that the temperature readings at indices 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 all exceed their immediate neighbors.
Input
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Output
[]
Explanation: Step-by-step: Given the input [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], we first initialize an empty list to store the indices of temperature readings that exceed their immediate neighbors. Then, we iterate through the input array. For each element, we check if it exceeds its immediate neighbor. Since all elements are equal, none of them exceed their immediate neighbors. Therefore, we return an empty list.
Constraints
- 1 <= array length <= 1000
- -10000 <= temperature reading <= 10000
Optimal Approach & Strategy
The optimal approach involves iterating through the array and checking each element with its adjacent elements, resulting in a linear time complexity of O(n). This approach is more efficient and suitable for large arrays.
Brute Force Approach
The brute-force approach involves comparing each element with all other elements, resulting in a time complexity of O(n²). This approach is inefficient and not recommended for large arrays.
Verified Code Solutions
function peakTemperatureIndices(temperatures) {
let result = [];
for (let i = 0; i < temperatures.length - 1; i++) {
if (i === 0 && temperatures[i] > temperatures[i + 1]) {
result.push(i);
} else if (i === temperatures.length - 2 && temperatures[i] > temperatures[i - 1]) {
result.push(i);
} else if (temperatures[i] > temperatures[i - 1] && temperatures[i] > temperatures[i + 1]) {
result.push(i);
}
}
return result;
}class Solution {
public int[] peakTemperatureIndices(int[] temperatures) {
int[] indices = new int[temperatures.length];
int index = 0;
for (int i = 1; i < temperatures.length - 1; i++) {
if (temperatures[i] > temperatures[i - 1] && temperatures[i] > temperatures[i + 1]) {
indices[index++] = i;
}
}
if (temperatures[0] > temperatures[1]) {
indices[index++] = 0;
}
if (temperatures[temperatures.length - 1] > temperatures[temperatures.length - 2]) {
indices[index++] = temperatures.length - 1;
}
int[] result = new int[index];
System.arraycopy(indices, 0, result, 0, index);
return result;
}
}def peak_temperature_indices(temperatures):
indices = []
for i in range(1, len(temperatures) - 1):
if temperatures[i] > temperatures[i - 1] and temperatures[i] > temperatures[i + 1]:
indices.append(i)
if temperatures[0] > temperatures[1]:
indices.append(0)
if temperatures[-1] > temperatures[-2]:
indices.append(len(temperatures) - 1)
return indicesfunction peakTemperatureIndices(temperatures) {
let result = [];
for (let i = 0; i < temperatures.length - 1; i++) {
if (i === 0 && temperatures[i] > temperatures[i + 1]) {
result.push(i);
} else if (i === temperatures.length - 2 && temperatures[i] > temperatures[i - 1]) {
result.push(i);
} else if (temperatures[i] > temperatures[i - 1] && temperatures[i] > temperatures[i + 1]) {
result.push(i);
}
}
return result;
}Asked in Top Tech Interviews
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.