Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total sum of the array.
DSA C#
int totalSum = 0; foreach (int num in nums) { totalSum [1]= num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes subtraction instead of addition.
Using '*' or '/' will not accumulate the sum correctly.
✗ Incorrect
We add each number to totalSum to get the sum of all elements.
2fill in blank
mediumComplete the code to check if the total sum is odd and return false if so.
DSA C#
if (totalSum [1] 2 != 0) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' will perform division, not remainder check.
Using '*' or '+' will not correctly check oddness.
✗ Incorrect
Using '%' checks the remainder when dividing by 2 to determine if the sum is odd.
3fill in blank
hardFix the error in the initialization of the boolean array for dynamic programming.
DSA C#
bool[] dp = new bool[[1] + 1]; dp[0] = true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using totalSum or nums.Length causes array size to be too large or incorrect.
Using nums.Sum() is redundant and may cause confusion.
✗ Incorrect
We only need to check sums up to half of totalSum for partitioning.
4fill in blank
hardFill both blanks to update the dp array correctly inside the loop.
DSA C#
for (int i = 0; i < nums.Length; i++) { for (int j = [1]; j >= [2]; j--) { dp[j] = dp[j] || dp[j - nums[i]]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping forward causes incorrect dp updates.
Using wrong bounds causes index errors or logic errors.
✗ Incorrect
We iterate j from target down to nums[i] to update dp correctly without overwriting needed values.
5fill in blank
hardFill all three blanks to complete the method that returns if partition is possible.
DSA C#
public bool CanPartition(int[] nums) {
int totalSum = 0;
foreach (int num in nums) {
totalSum [1]= num;
}
if (totalSum [2] 2 != 0) return false;
int target = totalSum / 2;
bool[] dp = new bool[target + 1];
dp[0] = true;
for (int i = 0; i < nums.Length; i++) {
for (int j = target; j >= nums[i]; j--) {
dp[j] = dp[j] || dp[j - nums[i]];
}
}
return dp[[3]];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' in sum accumulation.
Using '/' instead of '%' for odd check.
Returning dp[totalSum] instead of dp[target].
✗ Incorrect
We add numbers to totalSum, check if totalSum % 2 != 0, and return dp[target] at the end.
