Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of inversion count for a small array
What is the output of the following JavaScript code that counts inversions in an array?
DSA Javascript
function countInversions(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) count++;
}
}
return count;
}
console.log(countInversions([2, 4, 1, 3, 5]));Attempts:
2 left
š” Hint
Count pairs where a number appears before a smaller number.
ā Incorrect
The pairs that are inverted are (2,1), (4,1), and (4,3), totaling 3 inversions.
ā Predict Output
intermediateOutput of inversion count using merge sort method
What is the output of this JavaScript code that counts inversions using a merge sort approach?
DSA Javascript
function mergeSort(arr) {
if (arr.length <= 1) return {sorted: arr, count: 0};
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
const merged = [];
let i = 0, j = 0, count = left.count + right.count;
while (i < left.sorted.length && j < right.sorted.length) {
if (left.sorted[i] <= right.sorted[j]) {
merged.push(left.sorted[i++]);
} else {
merged.push(right.sorted[j++]);
count += left.sorted.length - i;
}
}
while (i < left.sorted.length) merged.push(left.sorted[i++]);
while (j < right.sorted.length) merged.push(right.sorted[j++]);
return {sorted: merged, count};
}
const result = mergeSort([8, 4, 2, 1]);
console.log(result.count);Attempts:
2 left
š” Hint
Count how many elements from left remain when picking from right during merge.
ā Incorrect
The array [8,4,2,1] has 6 inversions: (8,4), (8,2), (8,1), (4,2), (4,1), (2,1).
š§ Conceptual
advancedUnderstanding inversion count complexity
What is the time complexity of counting inversions using the merge sort based method for an array of size n?
Attempts:
2 left
š” Hint
Think about how merge sort divides and merges the array.
ā Incorrect
The merge sort method divides the array into halves recursively and merges them counting inversions, leading to O(n log n) time.
š§ Debug
advancedIdentify the error in inversion count code
What error will this code produce when run?
DSA Javascript
function countInversions(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) count++;
}
}
return count;
}
console.log(countInversions([1, 3, 2]));Attempts:
2 left
š” Hint
Check the loop boundary for j.
ā Incorrect
The inner loop runs with j <= arr.length, so arr[j] is undefined when j equals arr.length, causing a TypeError when comparing.
š Application
expertCount inversions in a large array efficiently
You have an array of 100,000 integers. Which approach is best to count inversions efficiently?
Attempts:
2 left
š” Hint
Consider time complexity for large n.
ā Incorrect
Nested loops are too slow for large arrays. Merge sort counting is efficient and accurate. Sorting alone does not count inversions. Hash map cannot count inversions in O(n).
