Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Count inversions in a small array
What is the output of the following C code that counts inversions in the array?
DSA C
#include <stdio.h> int merge(int arr[], int temp[], int left, int mid, int right) { int i = left, j = mid, k = left, inv_count = 0; while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count += (mid - i); } } while (i <= mid - 1) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (i = left; i <= right; i++) arr[i] = temp[i]; return inv_count; } int mergeSort(int arr[], int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { mid = (right + left) / 2; inv_count += mergeSort(arr, temp, left, mid); inv_count += mergeSort(arr, temp, mid + 1, right); inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } int main() { int arr[] = {2, 4, 1, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); int temp[n]; int result = mergeSort(arr, temp, 0, n - 1); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Count pairs (i, j) where i < j and arr[i] > arr[j].
✗ Incorrect
The array has three inversions: (2,1), (4,1), and (4,3). The code uses merge sort to count these efficiently.
🧠 Conceptual
intermediate1:30remaining
Understanding inversion count concept
Which of the following best describes what an inversion in an array means?
Attempts:
2 left
💡 Hint
Think about pairs that are out of order.
✗ Incorrect
An inversion is when a bigger number appears before a smaller number in the array.
❓ Predict Output
advanced2:00remaining
Count inversions in a reversed array
What is the output of the following C code counting inversions in a reversed array?
DSA C
#include <stdio.h> int merge(int arr[], int temp[], int left, int mid, int right) { int i = left, j = mid, k = left, inv_count = 0; while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count += (mid - i); } } while (i <= mid - 1) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (i = left; i <= right; i++) arr[i] = temp[i]; return inv_count; } int mergeSort(int arr[], int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { mid = (right + left) / 2; inv_count += mergeSort(arr, temp, left, mid); inv_count += mergeSort(arr, temp, mid + 1, right); inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } int main() { int arr[] = {5, 4, 3, 2, 1}; int n = sizeof(arr) / sizeof(arr[0]); int temp[n]; int result = mergeSort(arr, temp, 0, n - 1); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Count all pairs where a bigger number appears before a smaller one in a reversed array.
✗ Incorrect
A reversed array of size 5 has maximum inversions: 5*4/2 = 10.
🔧 Debug
advanced2:00remaining
Identify the bug in inversion count code
What error will the following code produce when counting inversions?
DSA C
#include <stdio.h> int merge(int arr[], int temp[], int left, int mid, int right) { int i = left, j = mid, k = left, inv_count = 0; while ((i <= mid) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count += (mid - i + 1); } } while (i <= mid) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (i = left; i <= right; i++) arr[i] = temp[i]; return inv_count; } int mergeSort(int arr[], int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { mid = (right + left) / 2; inv_count += mergeSort(arr, temp, left, mid); inv_count += mergeSort(arr, temp, mid + 1, right); inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } int main() { int arr[] = {2, 3, 8, 6, 1}; int n = sizeof(arr) / sizeof(arr[0]); int temp[n]; int result = mergeSort(arr, temp, 0, n - 1); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check the merge function's loop conditions and indices carefully.
✗ Incorrect
The merge function uses wrong mid index boundaries causing it to miss counting one inversion, resulting in output 4 instead of 5.
🚀 Application
expert2:30remaining
Find number of inversions after multiple updates
Given an array and multiple update operations that change elements, which data structure is best suited to efficiently count inversions after each update?
Attempts:
2 left
💡 Hint
Think about data structures that support fast range queries and updates.
✗ Incorrect
Segment Trees or Binary Indexed Trees combined with merge sort logic allow efficient inversion counting after updates, unlike simple traversal which is too slow.
