Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateCount inversions using merge sort
What is the output of the following Java code that counts inversions in an array using merge sort?
DSA Java
public class InversionCount { public static int mergeSort(int[] arr, int[] temp, int left, int right) { int mid, invCount = 0; if (right > left) { mid = (right + left) / 2; invCount += mergeSort(arr, temp, left, mid); invCount += mergeSort(arr, temp, mid + 1, right); invCount += merge(arr, temp, left, mid + 1, right); } return invCount; } public static int merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left; int invCount = 0; while (i <= mid - 1 && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; invCount += (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 invCount; } public static void main(String[] args) { int[] arr = {2, 4, 1, 3, 5}; int[] temp = new int[arr.length]; int result = mergeSort(arr, temp, 0, arr.length - 1); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Count pairs where left element is greater than right element during merge.
✗ Incorrect
The code counts inversions by merging sorted halves and counting how many elements from the left half are greater than elements in the right half. For the array {2,4,1,3,5}, the inversions are (2,1), (4,1), and (4,3), totaling 3.
❓ Predict Output
intermediateCount inversions in a sorted array
What is the output of the following Java code counting inversions in a sorted array?
DSA Java
public class InversionCount { public static int mergeSort(int[] arr, int[] temp, int left, int right) { int mid, invCount = 0; if (right > left) { mid = (right + left) / 2; invCount += mergeSort(arr, temp, left, mid); invCount += mergeSort(arr, temp, mid + 1, right); invCount += merge(arr, temp, left, mid + 1, right); } return invCount; } public static int merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left; int invCount = 0; while (i <= mid - 1 && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; invCount += (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 invCount; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] temp = new int[arr.length]; int result = mergeSort(arr, temp, 0, arr.length - 1); System.out.println(result); } }
Attempts:
2 left
💡 Hint
A sorted array has no inversions.
✗ Incorrect
Since the array is already sorted in ascending order, there are no pairs where a left element is greater than a right element, so the inversion count is zero.
🔧 Debug
advancedIdentify the error in inversion count code
What error will the following Java code produce when run?
DSA Java
public class InversionCount { public static int mergeSort(int[] arr, int[] temp, int left, int right) { int mid, invCount = 0; if (right >= left) { mid = (right + left) / 2; invCount += mergeSort(arr, temp, left, mid); invCount += mergeSort(arr, temp, mid + 1, right); invCount += merge(arr, temp, left, mid + 1, right); } return invCount; } public static int merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left; int invCount = 0; while (i <= mid - 1 && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; invCount += (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 invCount; } public static void main(String[] args) { int[] arr = {2, 3, 8, 6, 1}; int[] temp = new int[arr.length]; int result = mergeSort(arr, temp, 0, arr.length - 1); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check the base condition for recursion termination.
✗ Incorrect
The base condition uses 'if (right >= left)' which is always true when right == left, causing infinite recursion and eventually StackOverflowError.
❓ Predict Output
advancedCount inversions in a reversed array
What is the output of the following Java code counting inversions in a reversed array?
DSA Java
public class InversionCount { public static int mergeSort(int[] arr, int[] temp, int left, int right) { int mid, invCount = 0; if (right > left) { mid = (right + left) / 2; invCount += mergeSort(arr, temp, left, mid); invCount += mergeSort(arr, temp, mid + 1, right); invCount += merge(arr, temp, left, mid + 1, right); } return invCount; } public static int merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left; int invCount = 0; while (i <= mid - 1 && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; invCount += (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 invCount; } public static void main(String[] args) { int[] arr = {5, 4, 3, 2, 1}; int[] temp = new int[arr.length]; int result = mergeSort(arr, temp, 0, arr.length - 1); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Count all pairs where left element is greater than right element in reversed array.
✗ Incorrect
A reversed array of size 5 has maximum inversions: 5*4/2 = 10. The code counts all these pairs correctly.
🧠 Conceptual
expertTime complexity of inversion count using merge sort
What is the time complexity of counting inversions in an array of size n using the merge sort based approach?
Attempts:
2 left
💡 Hint
Consider the divide and conquer nature of merge sort.
✗ Incorrect
The merge sort based inversion count divides the array and merges in O(n) time at each level, with log n levels, resulting in O(n log n) time complexity.
