Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Reverse Pairs

XPChallenge Points: 30
levelLevel: Hard

You are given an integer array nums. A reverse pair happens when an element on the left side is more than double an element on the right side.
Formally, for indices i < j, the pair (i, j) is a reverse pair if:

nums[i] > 2 * nums[j]

Your task is to count how many such valid reverse pairs exist in the array.

Example 1:

Input: nums = [4, 1, 8, 2]

Output: 2

Explanation:

Pairs: (0, 1) → 4 > 2 * 1 (2, 3) → 8 > 2 * 2 Total reverse pairs = 2

Example 2:

Input: nums = [10, 5, 2, 1]

Output: 6

Explanation:

All pairs (i, j) where i < j satisfy nums[i] > 2 * nums[j]: (0,1), (0,2), (0,3), (1,2), (1,3), (2,3) Total = 6

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result