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 Rust code that counts inversions in an array?
DSA Rust
fn count_inversions(arr: &mut [i32]) -> i32 {
let n = arr.len();
if n <= 1 {
return 0;
}
let mid = n / 2;
let mut inv_count = 0;
inv_count += count_inversions(&mut arr[..mid]);
inv_count += count_inversions(&mut arr[mid..]);
let mut merged = Vec::with_capacity(n);
let (mut i, mut j) = (0, mid);
while i < mid && j < n {
if arr[i] <= arr[j] {
merged.push(arr[i]);
i += 1;
} else {
merged.push(arr[j]);
inv_count += (mid - i) as i32;
j += 1;
}
}
while i < mid {
merged.push(arr[i]);
i += 1;
}
while j < n {
merged.push(arr[j]);
j += 1;
}
arr.copy_from_slice(&merged);
inv_count
}
fn main() {
let mut arr = [2, 4, 1, 3, 5];
let result = count_inversions(&mut arr);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Count pairs (i, j) where i < j and arr[i] > arr[j].
✗ Incorrect
The code uses a merge sort approach to count inversions. 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 reversed array
What is the output of this Rust code counting inversions in a reversed array?
DSA Rust
fn count_inversions(arr: &mut [i32]) -> i32 {
let n = arr.len();
if n <= 1 {
return 0;
}
let mid = n / 2;
let mut inv_count = 0;
inv_count += count_inversions(&mut arr[..mid]);
inv_count += count_inversions(&mut arr[mid..]);
let mut merged = Vec::with_capacity(n);
let (mut i, mut j) = (0, mid);
while i < mid && j < n {
if arr[i] <= arr[j] {
merged.push(arr[i]);
i += 1;
} else {
merged.push(arr[j]);
inv_count += (mid - i) as i32;
j += 1;
}
}
while i < mid {
merged.push(arr[i]);
i += 1;
}
while j < n {
merged.push(arr[j]);
j += 1;
}
arr.copy_from_slice(&merged);
inv_count
}
fn main() {
let mut arr = [5, 4, 3, 2, 1];
let result = count_inversions(&mut arr);
println!("{}", result);
}Attempts:
2 left
💡 Hint
A reversed array has the maximum number of inversions.
✗ Incorrect
For an array of size 5 reversed, the number of inversions is n*(n-1)/2 = 10.
🔧 Debug
advancedIdentify the error in inversion count code
What error does the following Rust code produce when counting inversions?
DSA Rust
fn count_inversions(arr: &mut [i32]) -> i32 {
let n = arr.len();
if n <= 1 {
return 0;
}
let mid = n / 2;
let mut inv_count = 0;
inv_count += count_inversions(&mut arr[..mid]);
inv_count += count_inversions(&mut arr[mid..]);
let mut merged = Vec::with_capacity(n);
let (mut i, mut j) = (0, mid);
while i < mid && j < n {
if arr[i] < arr[j] {
merged.push(arr[i]);
i += 1;
} else {
merged.push(arr[j]);
inv_count += (mid - i) as i32;
j += 1;
}
}
while i < mid {
merged.push(arr[i]);
i += 1;
}
while j < n {
merged.push(arr[j]);
j += 1;
}
arr.copy_from_slice(&merged);
inv_count
}
fn main() {
let mut arr = [2, 4, 1, 3, 5];
let result = count_inversions(&mut arr);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check the comparison operator in the merge step.
✗ Incorrect
The code uses '<' instead of '<=' in the merge comparison, causing it to miss counting some inversions.
🧠 Conceptual
advancedUnderstanding inversion count complexity
What is the time complexity of counting inversions using the merge sort approach on an array of size n?
Attempts:
2 left
💡 Hint
Think about how merge sort divides and merges the array.
✗ Incorrect
The merge sort approach divides the array and merges in O(n log n) time, counting inversions during merge.
🚀 Application
expertCount inversions in a large array efficiently
Given an array of 1,000,000 integers, which approach is best to count inversions efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity and scalability.
✗ Incorrect
Brute force is O(n^2) and too slow. Merge sort based counting is O(n log n) and efficient for large arrays.
