Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateCount Inversions Output for Small Array
What is the output of the following C++ code that counts inversions in an array?
DSA C++
#include <iostream> #include <vector> using namespace std; int mergeAndCount(vector<int>& arr, int left, int mid, int right) { int count = 0; int n1 = mid - left + 1; int n2 = right - mid; vector<int> leftArr(n1), rightArr(n2); for (int i = 0; i < n1; i++) leftArr[i] = arr[left + i]; for (int j = 0; j < n2; j++) rightArr[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; count += (n1 - i); } } while (i < n1) arr[k++] = leftArr[i++]; while (j < n2) arr[k++] = rightArr[j++]; return count; } int mergeSortAndCount(vector<int>& arr, int left, int right) { int count = 0; if (left < right) { int mid = left + (right - left) / 2; count += mergeSortAndCount(arr, left, mid); count += mergeSortAndCount(arr, mid + 1, right); count += mergeAndCount(arr, left, mid, right); } return count; } int main() { vector<int> arr = {2, 4, 1, 3, 5}; int result = mergeSortAndCount(arr, 0, arr.size() - 1); cout << result << endl; return 0; }
Attempts:
2 left
💡 Hint
Count pairs where a larger number appears before a smaller number.
✗ Incorrect
The inversions are (2,1), (4,1), and (4,3), totaling 3.
❓ Predict Output
intermediateCount Inversions Output for Sorted Array
What is the output of the following C++ code that counts inversions in a sorted array?
DSA C++
#include <iostream> #include <vector> using namespace std; int mergeAndCount(vector<int>& arr, int left, int mid, int right) { int count = 0; int n1 = mid - left + 1; int n2 = right - mid; vector<int> leftArr(n1), rightArr(n2); for (int i = 0; i < n1; i++) leftArr[i] = arr[left + i]; for (int j = 0; j < n2; j++) rightArr[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; count += (n1 - i); } } while (i < n1) arr[k++] = leftArr[i++]; while (j < n2) arr[k++] = rightArr[j++]; return count; } int mergeSortAndCount(vector<int>& arr, int left, int right) { int count = 0; if (left < right) { int mid = left + (right - left) / 2; count += mergeSortAndCount(arr, left, mid); count += mergeSortAndCount(arr, mid + 1, right); count += mergeAndCount(arr, left, mid, right); } return count; } int main() { vector<int> arr = {1, 2, 3, 4, 5}; int result = mergeSortAndCount(arr, 0, arr.size() - 1); cout << result << endl; return 0; }
Attempts:
2 left
💡 Hint
A sorted array has no inversions.
✗ Incorrect
Since the array is sorted ascending, no pairs are inverted.
🔧 Debug
advancedIdentify the Bug in Inversion Count Merge Function
The following merge function is intended to count inversions during merge sort. What is the bug causing incorrect inversion count?
DSA C++
int mergeAndCount(vector<int>& arr, int left, int mid, int right) { int count = 0; int n1 = mid - left + 1; int n2 = right - mid; vector<int> leftArr(n1), rightArr(n2); for (int i = 0; i < n1; i++) leftArr[i] = arr[left + i]; for (int j = 0; j < n2; j++) rightArr[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; count += (n1 - i); } } while (i < n1) arr[k++] = leftArr[i++]; while (j < n2) arr[k++] = rightArr[j++]; return count; }
Attempts:
2 left
💡 Hint
Check array indexing in the copying loops.
✗ Incorrect
Using <= causes the loops to access one element beyond array bounds, leading to undefined behavior and wrong counts.
🧠 Conceptual
advancedWhy Use Merge Sort to Count Inversions?
Why is merge sort a good choice to count inversions in an array efficiently?
Attempts:
2 left
💡 Hint
Think about how merge sort combines sorted halves.
✗ Incorrect
Merge sort counts inversions while merging two sorted halves, achieving O(n log n) time, better than naive O(n^2).
🚀 Application
expertCount Inversions in Large Array with Duplicates
Given the array [3, 3, 2, 2, 1, 1], what is the total number of inversions counted by the standard merge sort inversion count algorithm?
Attempts:
2 left
💡 Hint
Count pairs (i, j) where i < j and arr[i] > arr[j], considering duplicates.
✗ Incorrect
Inversions are (3,2), (3,1), (2,1) pairs counted with duplicates. Total is 12.
