Count Inversions in Array in DSA Java - Time & Space Complexity
We want to understand how long it takes to count inversions in an array as the array grows.
The question is: how does the work increase when the array size gets bigger?
Analyze the time complexity of the following code snippet.
public class InversionCounter {
public static int countInversions(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
count++;
}
}
}
return count;
}
}
This code counts how many pairs of elements are out of order in the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing pairs of elements to check if one is greater than the other.
- How many times: The outer loop runs n-1 times, and for each outer loop, the inner loop runs fewer times, roughly n times in total.
As the array size grows, the number of comparisons grows much faster.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons |
| 100 | About 4,950 comparisons |
| 1000 | About 499,500 comparisons |
Pattern observation: The number of operations grows roughly with the square of the input size.
Time Complexity: O(n²)
This means if the array doubles in size, the work to count inversions roughly quadruples.
[X] Wrong: "Counting inversions can be done in linear time by just one pass through the array."
[OK] Correct: Because each element must be compared with many others to find all inversions, one pass is not enough.
Understanding this helps you explain how nested loops affect performance and why more efficient methods exist.
"What if we used a merge sort based approach to count inversions? How would the time complexity change?"
Practice
inversion mean in the context of an array?Solution
Step 1: Understand the definition of inversion
An inversion is when a larger number appears before a smaller number in the array.Step 2: Compare options with definition
A pair of elements where the first is greater than the second and appears before it correctly describes this as a pair where the first element is greater and comes before the second.Final Answer:
A -> Option AQuick Check:
Inversion = pair (i, j) with i < j and arr[i] > arr[j] [OK]
- Confusing inversion with sorted order
- Thinking inversion counts equal elements
- Mixing up element positions
arr with values 3, 1, 2?Solution
Step 1: Check array declaration syntax
In Java, arrays are declared with square brackets and initialized with curly braces or using new keyword with curly braces.Step 2: Validate each option
int[] arr = {3, 1, 2}; uses correct syntax: int[] arr = {3, 1, 2}; int arr = {3, 1, 2}; misses brackets, int[] arr = new int(3, 1, 2); uses wrong constructor syntax, int arr[] = new int[3]; does not initialize to the given values (zeros instead).Final Answer:
B -> Option CQuick Check:
Correct array init = int[] arr = {3, 1, 2}; [OK]
- Missing square brackets in declaration
- Using parentheses instead of braces
- Incorrect use of new keyword
[2, 4, 1, 3, 5]?Solution
Step 1: Identify all inversion pairs
Pairs where first element is greater and appears before second: (2,1), (4,1), (4,3), (4,5) is not inversion, (3,5) no inversion. Actually, count carefully: (2,1), (4,1), (4,3) are 3 inversions. But check if (2,3) or (2,5) are inversions? No. So total 3 inversions.Step 2: Count the pairs
Counting these pairs gives 3 inversions.Final Answer:
3 -> Option DQuick Check:
Inversions in [2,4,1,3,5] = 3 [OK]
- Counting pairs with equal elements
- Missing some inversion pairs
- Counting sorted pairs as inversions
public static int countInversions(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
count++;
}
}
}
return count;
}Solution
Step 1: Analyze the inversion condition
Inversion means arr[i] > arr[j] where i < j, but code checks arr[i] < arr[j], which is incorrect.Step 2: Identify the fix
Change condition to arr[i] > arr[j] to correctly count inversions.Final Answer:
The comparison should be arr[i] > arr[j] to count inversions -> Option AQuick Check:
Correct condition = arr[i] > arr[j] [OK]
- Using wrong comparison operator
- Starting inner loop incorrectly
- Resetting count inside loops
[8, 4, 2, 1], what is the total number of inversions? Also, explain why this number is significant in sorting algorithms.Solution
Step 1: Count inversions in the array
Pairs: (8,4), (8,2), (8,1), (4,2), (4,1), (2,1) total 6 inversions.Step 2: Understand significance in sorting
Maximum inversions mean array is sorted in descending order, which is worst case for some sorting algorithms.Final Answer:
6; It shows the array is sorted in descending order -> Option BQuick Check:
Inversions = 6; descending order = max inversions [OK]
- Miscounting inversion pairs
- Confusing max inversions with sorted array
- Ignoring significance in sorting
Ready to prove it?
Pass the test and this pattern moves unknown
