FunctionRacerProblemsRace

Out-of-Order Pairs

EasySorting and searchingPython, JavaScript, TypeScript, Java, C++

A pair of positions i < j in nums is out of order when nums[i] > nums[j]: the earlier item is strictly bigger than the later one. Return how many out-of-order pairs nums has.

A list in non-decreasing order has none, and a strictly decreasing list of n items has n * (n - 1) / 2. Equal items never form an out-of-order pair.

Example 1

nums [3,1,2]
returns 2

3 comes before 1 and before 2, and both are smaller: the pairs (0, 1) and (0, 2).

Example 2

nums [1,2,3]
returns 0

Nothing is out of order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems