A stream of n signed measurements is given. A pair of positions (i, j) with i < j is an anomaly when value[i] > 2 * value[j]. Count how many anomaly pairs exist.
Line 1: an integer n.
Line 2: n space-separated integers, the measurements.
A single integer: the number of anomaly pairs.
Example 1
Input
4 6 1 2 7
Expected
2
Explanation
Pairs with value[i] > 2*value[j]: (6,1) since 6>2 and (6,2) since 6>4. That is 2 anomalies.
Example 2
Input
5 1 2 3 4 5
Expected
0
Explanation
The values increase, so no earlier value exceeds twice a later one: 0 anomalies.
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 →