All practice tasks

Coding (Algo)

Count Out of Order Record Pairs

Divide and conquer · Medium

A migration check compares the arrival order of numeric record keys with ascending order. It needs a count of all out-of-order pairs as a measure of disorder. Counting only adjacent swaps without the total pair relationship is insufficient.

Count pairs of original indexes i < j for which the earlier value is strictly greater than the later value.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ nums: integer[] }

Fields

  • nums is an array of signed integers. Repeated values at different positions are separate entries.

Output

Return value

  • Return one integer pair count. Equal values are not inversions.
  • Count pairs by original indexes. Empty and already sorted arrays return 0.

Constraints

  • 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
  • All totals and counts fit exactly in a safe JSON integer.

Examples

Example 1

{
  "nums": [
    3,
    1,
    2
  ]
}

Returns

2

Example 2

{
  "nums": [
    2,
    2,
    1
  ]
}

Returns

2

Required complexity

O(n log(n + 1)) time and O(n) auxiliary space

What this task practices

Count Out of Order Record Pairs is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.