Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total sum of the array.
DSA Java
int sum = 0; for (int num : nums) { sum [1] num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' will subtract numbers instead of adding.
Using '*=' or '/=' will multiply or divide, which is incorrect here.
✗ Incorrect
We use '+=' to add each number to the sum variable.
2fill in blank
mediumComplete the code to check if the total sum is odd.
DSA Java
if (sum [1] 2 != 0) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' checks division but not remainder.
Using '*' or '-' does not check odd or even.
✗ Incorrect
The '%' operator gives the remainder. If sum % 2 is not zero, sum is odd.
3fill in blank
hardFix the error in the initialization of the boolean array for dynamic programming.
DSA Java
boolean[] dp = new boolean[[1] + 1]; dp[0] = true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of sum/2 wastes space.
Using nums.length is unrelated to sum values.
✗ Incorrect
We only need to check sums up to half of the total sum.
4fill in blank
hardFill both blanks to update the dp array correctly inside the loop.
DSA Java
for (int num : nums) { for (int i = [1]; i >= [2]; i--) { dp[i] = dp[i] || dp[i - num]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping upwards causes incorrect dp updates.
Using 0 as the start index breaks the logic.
✗ Incorrect
We loop from target down to num to update dp correctly without overwriting needed values.
5fill in blank
hardFill both blanks to return the final result from dp array.
DSA Java
int target = sum [1] 2; return dp[sum [2] 2]; // target is half of sum // dp[target] tells if subset sum is possible
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition instead of division.
Returning dp at wrong index.
✗ Incorrect
We divide sum by 2 to get target and return dp[target] to check if subset sum exists.
