Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateCount inversions in a small array
What is the output of the following C# code that counts inversions in the array?
DSA C#
using System; class Program { 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; } static void Main() { int[] array = {2, 4, 1, 3, 5}; Console.WriteLine(CountInversions(array)); } }
Attempts:
2 left
💡 Hint
Count pairs (i, j) where i < j and arr[i] > arr[j].
✗ Incorrect
The pairs are (2,1), (4,1), and (4,3). Total 3 inversions.
🧠 Conceptual
intermediateUnderstanding inversion count meaning
What does the inversion count in an array represent?
Attempts:
2 left
💡 Hint
Think about pairs that are in the wrong order.
✗ Incorrect
Inversion count is the number of pairs (i, j) such that i < j and arr[i] > arr[j].
❓ Predict Output
advancedOutput of merge sort based inversion count
What is the output of this C# code that uses merge sort to count inversions?
DSA C#
using System; class Program { static int Merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left, invCount = 0; while (i < mid && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; invCount += (mid - i); } } while (i < mid) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (int idx = left; idx <= right; idx++) arr[idx] = temp[idx]; return invCount; } static int MergeSort(int[] arr, int[] temp, int left, int right) { int invCount = 0; if (left < right) { int mid = (left + right) / 2; invCount += MergeSort(arr, temp, left, mid); invCount += MergeSort(arr, temp, mid + 1, right); invCount += Merge(arr, temp, left, mid + 1, right); } return invCount; } static void Main() { int[] array = {8, 4, 2, 1}; int[] temp = new int[array.Length]; Console.WriteLine(MergeSort(array, temp, 0, array.Length - 1)); } }
Attempts:
2 left
💡 Hint
Count pairs where left element is bigger than right element in the array.
✗ Incorrect
The inversions are (8,4), (8,2), (8,1), (4,2), (4,1), and (2,1) totaling 6.
🔧 Debug
advancedIdentify the bug in inversion count code
What error will this C# code produce when counting inversions?
DSA C#
using System; class Program { 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; } static void Main() { int[] array = {1, 3, 2}; Console.WriteLine(CountInversions(array)); } }
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The outer loop uses i <= arr.Length which causes i to go out of bounds when accessing arr[i].
🚀 Application
expertMinimum swaps to sort array using inversion count concept
Given an array, what is the minimum number of swaps required to sort it? Consider the array [4, 3, 2, 1]. Which option correctly gives the minimum swaps?
Attempts:
2 left
💡 Hint
Minimum swaps to sort is related to cycles in the permutation, not just inversion count.
✗ Incorrect
The array [4,3,2,1] requires 3 swaps to sort: swap 4 and 1, then 3 and 2, then 4 and 2.
