A monitoring tool flags "regression pairs" in a series of request latencies: a pair of positions i < j where the earlier latency was more than twice the later one, latencies[i] > 2 * latencies[j].
Write countSpikePairs(latencies) that returns how many such pairs exist. The series can hold up to 400,000 latencies, so checking every pair is too slow; aim for O(n log n).
Input: latencies = [1,3,2,3,1] Output: 2
3 > 2 × 1 for both 3s paired with the last 1.
Input: latencies = [2,4,3,5,1] Output: 3
4, 3, and 5 are each more than twice the final 1. 2 is exactly twice, which does not count.
0 <= latencies.length <= 4 * 10^5-10^9 <= latencies[i] <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #195 Latency Spikes Over Double. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.