Skip to the code
cf.
+50 XP

#195 Latency Spikes Over Double

hard

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 be long, so aim for O(n log n).

Example 1

Input: latencies = [1,3,2,3,1]
Output: 2

3 > 2 × 1 for both 3s paired with the last 1.

Example 2

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.

Constraints

  • 0 <= latencies.length <= 50000
  • -10^9 <= latencies[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).