Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the number of inversions in the array using a simple nested loop.
DSA C++
int countInversions(vector<int>& arr) {
int count = 0;
for (int i = 0; i < arr.size(); i++) {
for (int j = i + 1; j < arr.size(); j++) {
if (arr[i] [1] arr[j]) {
count++;
}
}
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will count non-inversions.
Using '==' will not count inversions correctly.
✗ Incorrect
We count an inversion when a larger number appears before a smaller number, so the condition is arr[i] > arr[j].
2fill in blank
mediumComplete the merge function to count inversions while merging two sorted halves.
DSA C++
int merge(vector<int>& arr, int left, int mid, int right) {
vector<int> temp;
int i = left, j = mid + 1, inv_count = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp.push_back(arr[i++]);
} else {
temp.push_back(arr[j++]);
inv_count += [1];
}
}
while (i <= mid) temp.push_back(arr[i++]);
while (j <= right) temp.push_back(arr[j++]);
for (int k = left; k <= right; k++) arr[k] = temp[k - left];
return inv_count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using j - mid counts wrong elements.
Using right - j + 1 counts elements from the right half incorrectly.
✗ Incorrect
When arr[i] > arr[j], all elements from i to mid are greater than arr[j], so add mid - i + 1 to inversion count.
3fill in blank
hardFix the error in the recursive mergeSort function to correctly count inversions.
DSA C++
int mergeSort(vector<int>& arr, int left, int right) {
int inv_count = 0;
if (left < right) {
int mid = left + (right - left) / 2;
inv_count += mergeSort(arr, left, mid);
inv_count += mergeSort(arr, mid + 1, right);
inv_count += [1];
}
return inv_count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping parameters causes wrong merges and wrong counts.
Using mid as right or left incorrectly breaks the recursion.
✗ Incorrect
The merge function must be called with left, mid, right to merge the two halves correctly and count inversions.
4fill in blank
hardComplete the countInversions function to use the mergeSort helper for efficient inversion counting.
DSA C++
int countInversions(vector<int>& arr) {
if (arr.size() == 0) return 0;
return [1](arr, 0, arr.[2] - 1);
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling merge directly doesn't recurse.
Using wrong range like arr.size() without -1 causes issues.
✗ Incorrect
Call the recursive mergeSort function with the full array range: mergeSort(arr, 0, arr.size() - 1).
5fill in blank
hardFill the two blanks to complete the merge function that counts inversions and merges two halves.
DSA C++
int merge(vector<int>& arr, int left, int mid, int right) {
vector<int> temp;
int i = left, j = mid + 1, inv_count = 0;
while (i <= mid && j <= right) {
if (arr[i] [1] arr[j]) {
temp.push_back(arr[i++]);
} else {
temp.push_back(arr[j++]);
inv_count += [2];
}
}
while (i <= mid) temp.push_back(arr[i++]);
while (j <= right) temp.push_back(arr[j++]);
for (int k = left; k <= right; k++) arr[k] = temp[k - left];
return inv_count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' (B) in if-condition would pick larger first, breaking sort.
Using 'right - j + 1' (D) counts wrong half.
✗ Incorrect
Use '<=' (A) to check if arr[i] <= arr[j]; if not, all remaining elements in left half (mid - i + 1, C) form inversions with arr[j].
