Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total sum of the input vector.
DSA Rust
let total_sum: i32 = nums.iter().[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using map() instead of sum(), which only transforms elements.
Using fold() without initial value, causing errors.
✗ Incorrect
The sum() method adds all elements in the iterator, giving the total sum.
2fill in blank
mediumComplete the code to check if total_sum is odd and return false if so.
DSA Rust
if total_sum [1] 2 != 0 { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%).
Using equality (==) instead of modulus.
✗ Incorrect
Using % (modulus) checks if total_sum divided by 2 leaves a remainder, indicating oddness.
3fill in blank
hardFix the error in the code to initialize the dp vector with false values.
DSA Rust
let mut dp = vec![[1]; (target + 1) as usize];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with true, which breaks logic.
Using 0 or None, which are invalid for boolean vector.
✗ Incorrect
The dp vector should be initialized with false to indicate no sums are possible initially.
4fill in blank
hardFill both blanks to update dp vector correctly inside the loop.
DSA Rust
for num in nums.iter() { for j in (*[1]..=[2]).rev() { if dp[(j - *num) as usize] { dp[j as usize] = true; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping forward causing incorrect dp updates.
Using wrong range bounds.
✗ Incorrect
The inner loop runs backward from target down to num to avoid using updated dp values in the same iteration.
5fill in blank
hardFill all three blanks to complete the function that returns if partition is possible.
DSA Rust
fn can_partition(nums: Vec<i32>) -> bool {
let total_sum: i32 = nums.iter().[1]();
if total_sum [2] 2 != 0 {
return false;
}
let target = total_sum / 2;
let mut dp = vec![false; (target + 1) as usize];
dp[0] = true;
for num in nums.iter() {
for j in (*[3]..=target).rev() {
if j - *num >= 0 && dp[(j - *num) as usize] {
dp[j as usize] = true;
}
}
}
dp[target as usize]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method instead of sum().
Checking oddness incorrectly.
Looping from 0 instead of num.
✗ Incorrect
sum() calculates total, % checks oddness, and num is used as lower bound in loop.
