Count index pairs where an earlier value is more than twice a later value

Quick Overview

Count the index pairs in an integer array where an earlier element is more than twice a later one. Tests whether you can count them in O(n log n) time instead of checking every pair, and whether you handle the strict inequality, negative values and integer overflow correctly.

Count index pairs where an earlier value is more than twice a later value

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an integer array `nums`, return the number of reverse pairs in it. A reverse pair is a pair of indices `(i, j)` with `0 <= i < j < len(nums)` and `nums[i] > 2 * nums[j]`. ### Function Signature ```python def reverse_pairs(nums: list[int]) -> int: ``` ### Rules - Pairs are counted by index, so equal values at different positions form different pairs. - The inequality is strict. A pair with `nums[i] == 2 * nums[j]` does not count. - Values can be negative, and `2 * nums[j]` can fall outside the 32-bit range. The comparison must use the exact doubled value. ### Constraints - `1 <= len(nums) <= 5 * 10^4` - `-2^31 <= nums[i] <= 2^31 - 1` ### Examples ```text Input: nums = [1, 3, 2, 3, 1] Output: 2 ``` The pairs are (1, 4) and (3, 4), since `3 > 2 * 1` in both. Index 2 holds 2, which equals `2 * 1`, so (2, 4) does not count. No earlier element is larger than 4 or 6, so no pair ends at index 1, 2 or 3. ```text Input: nums = [2, 4, 3, 5, 1] Output: 3 ``` Only the last element is small enough to be the second half of a pair: 4, 3 and 5 at indices 1, 2 and 3 are each greater than `2 * 1 = 2`, while `nums[0] = 2` is not. ```text Input: nums = [-5, -3, 0] Output: 1 ``` Doubling a negative number makes it smaller: `2 * (-3) = -6` and `-5 > -6`, so (0, 1) counts even though -5 is less than -3. Neither -5 nor -3 is greater than `2 * 0 = 0`.

Overview: Count the index pairs in an integer array where an earlier element is more than twice a later one. Tests whether you can count them in O(n log n) time instead of checking every pair, and whether you handle the strict inequality, negative values and integer overflow correctly.

PracHub logo
General
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return the number of reverse pairs in it. A reverse pair is a pair of indices (i, j) with 0 <= i < j < len(nums) and nums[i] > 2 * nums[j].

Function Signature

def reverse_pairs(nums: list[int]) -> int:

Rules

  • Pairs are counted by index, so equal values at different positions form different pairs.
  • The inequality is strict. A pair with nums[i] == 2 * nums[j] does not count.
  • Values can be negative, and 2 * nums[j] can fall outside the 32-bit range. The comparison must use the exact doubled value.

Constraints

  • 1 <= len(nums) <= 5 * 10^4
  • -2^31 <= nums[i] <= 2^31 - 1

Examples

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

The pairs are (1, 4) and (3, 4), since 3 > 2 * 1 in both. Index 2 holds 2, which equals 2 * 1, so (2, 4) does not count. No earlier element is larger than 4 or 6, so no pair ends at index 1, 2 or 3.

Input: nums = [2, 4, 3, 5, 1]
Output: 3

Only the last element is small enough to be the second half of a pair: 4, 3 and 5 at indices 1, 2 and 3 are each greater than 2 * 1 = 2, while nums[0] = 2 is not.

Input: nums = [-5, -3, 0]
Output: 1

Doubling a negative number makes it smaller: 2 * (-3) = -6 and -5 > -6, so (0, 1) counts even though -5 is less than -3. Neither -5 nor -3 is greater than 2 * 0 = 0.