A seismology lab streams amplitude readings from a single seismograph, one integer per sampled instant, in chronological order. The lab defines a "tremor triad" as three sampled instants i < j < k such that the amplitude at instant j is strictly greater than the amplitude at instant i, and also strictly greater than the amplitude at instant k (a single rising-then-falling spike). Among all tremor triads present in the stream, report the smallest possible sum of the three amplitudes involved.
Print a single integer: the minimum value of a[i] + a[j] + a[k] over all valid tremor triads (indices i < j < k with a[i] < a[j] and a[k] < a[j]). If no tremor triad exists in the stream, print -1.
Example 1
Input
6 7 1 5 2 6 3
Expected
8
Explanation
The triple at indices (1,2,3) has values (1,5,2): 1 < 5 and 2 < 5, so it is a valid tremor triad with sum 1+5+2=8. No other valid triad has a smaller sum, so the answer is 8.
Example 2
Input
5 1 2 3 4 5
Expected
-1
Explanation
The readings are strictly increasing, so for every index j every later index k has a[k] > a[j]; no index ever has a strictly smaller reading after it, so no tremor triad exists and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →