Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total sum of the array elements.
DSA C
int totalSum = 0; for (int i = 0; i < n; i++) { totalSum [1] nums[i]; }
Drag options to blanks, or click blank then click option'
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 element to totalSum using the '+=' operator to get the total sum.
2fill in blank
mediumComplete the code to check if the total sum is odd, which means partition is impossible.
DSA C
if (totalSum [1] 2 != 0) { return false; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' will not check odd/even correctly.
Using '==' or '!=' without modulus will cause logic errors.
✗ Incorrect
The modulus operator '%' checks if totalSum is odd by checking remainder when divided by 2.
3fill in blank
hardFix the error in the recursive function call to check subset sum.
DSA C
bool canPartition(int* nums, int n, int sum) {
if (sum == 0) return true;
if (n == 0 || sum < 0) return false;
return canPartition(nums, n - 1, sum) || canPartition(nums, n - 1, [1] - nums[n - 1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding nums[n-1] instead of subtracting causes wrong sum calculation.
Passing 'n' instead of 'sum' causes type mismatch.
✗ Incorrect
The first recursive call keeps sum unchanged, the second subtracts nums[n-1] from sum.
4fill in blank
hardFill both blanks to create a dynamic programming table initialization for subset sum.
DSA C
bool dp[n + 1][sum + 1]; for (int i = 0; i <= n; i++) { dp[i][0] = [1]; } for (int j = 1; j <= sum; j++) { dp[0][j] = [2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i][0] to false breaks the base case.
Setting dp[0][j] to true allows impossible sums.
✗ Incorrect
Sum 0 can always be formed (true), but with 0 elements no positive sum can be formed (false).
5fill in blank
hardFill the blank to complete the DP state update for subset sum problem.
DSA C
for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum; j++) { if (nums[i - 1] <= j) { dp[i][j] = dp[i - 1][j] || dp[i - 1][[1]]; } else { dp[i][j] = dp[i - 1][j]; } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding nums[i-1] instead of subtracting causes wrong indexing.
Using wrong indices for dp causes logic errors.
✗ Incorrect
We check if sum j can be formed without current number or by including it (sum j - nums[i-1]).