Challenge - 5 Problems
Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateCount inversions in a small array
What is the output of the following TypeScript code that counts inversions in the array?
DSA Typescript
function countInversions(arr: number[]): number {
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 (i, j) where i < j and arr[i] > arr[j].
ā Incorrect
The inversions are (2,1), (4,1), and (4,3), totaling 3.
ā Predict Output
intermediateCount inversions using merge sort approach
What is the output of this TypeScript code that counts inversions using a merge sort method?
DSA Typescript
function mergeSortCount(arr: number[]): [number[], number] {
if (arr.length <= 1) return [arr, 0];
const mid = Math.floor(arr.length / 2);
const [left, leftCount] = mergeSortCount(arr.slice(0, mid));
const [right, rightCount] = mergeSortCount(arr.slice(mid));
let i = 0, j = 0, count = leftCount + rightCount;
const merged: number[] = [];
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
merged.push(left[i++]);
} else {
merged.push(right[j++]);
count += left.length - i;
}
}
return [merged.concat(left.slice(i)).concat(right.slice(j)), count];
}
const [sorted, inversions] = mergeSortCount([8, 4, 2, 1]);
console.log(inversions);Attempts:
2 left
š” Hint
Count how many elements from left are greater than elements from right during merge.
ā Incorrect
The inversions are (8,4), (8,2), (8,1), (4,2), (4,1), and (2,1), totaling 6.
š§ Debug
advancedIdentify the error in inversion count code
What error does this TypeScript code produce when counting inversions?
DSA Typescript
function countInversions(arr: number[]): number {
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 conditions carefully.
ā Incorrect
The inner loop uses j <= arr.length, which accesses arr[arr.length], undefined, causing a TypeError when comparing.
š§ Conceptual
advancedTime complexity of counting inversions using merge sort
What is the time complexity of counting inversions in an array of size n using the merge sort approach?
Attempts:
2 left
š” Hint
Think about how merge sort divides and merges the array.
ā Incorrect
Merge sort divides the array into halves recursively and merges them in O(n) time each level, resulting in O(n log n) overall.
š Application
expertNumber of inversions after swapping two elements
Given the array [3, 1, 2, 5, 4], what is the number of inversions after swapping elements at indices 1 and 4?
Attempts:
2 left
š” Hint
First count inversions in original array, then count after swap.
ā Incorrect
Original inversions: (3,1), (3,2), (5,4) = 3. After swapping elements at indices 1 and 4, array becomes [3,4,2,5,1]. New inversions: (3,2), (3,1), (4,2), (4,1), (2,1), (5,1) = 6.
