S
All challenges
Practice
Home
Blog
Practice
Examples
Feedback
Count Inversions in Array
00:00
Start
00:00
Sign in
Problem
Hints
3
Solution
History
Medium
Sorting
Divide & Conquer
Merge Sort
Amazon
Google
Count the number of inversions in an array — pairs (i,j) where i<j but arr[i]>arr[j].
Examples
Example 1
Input →
[2,4,1,3,5]
Output →
3
Note:
pairs: 2,1,4,1,4,3
Example 2
Input →
[1,2,3,4,5]
Output →
0
Note:
sorted
Example 3
Input →
[5,4,3,2,1]
Output →
10
Note:
reverse sorted: n*n-1/2
JavaScript
Python
Java (Coming soon)
C (Coming soon)
C++ (Coming soon)
Reset
Run
Submit
Cases
Results