Skip to content

ORIGINAL CODING QUESTION

Crossed arrival pairs

Count pairs i < j for which value[i] > value[j]. Equal values do not count.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n integer values. Print one integer as described.

Constraints

1 ≤ n ≤ 200; -100 ≤ value ≤ 100. Inputs contain exactly the stated values.

Example 1

Input

4 3 1 2 1

Output

4

There are four inversions.

Example 2

Input

3 1 1 1

Output

0

Equal values create none.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

A decreasing sequence creates many inversions.

Hint 2

A merge can count many crossed pairs at once.

Hint 3

When the right value is smaller, count all unmerged values on the left.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →