Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the total sum is even.
DSA Python
total_sum = sum(nums) if total_sum [1] 2 != 0: return False
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '%' to check evenness.
Using '//' which is floor division, not modulo.
✗ Incorrect
We use the modulo operator '%' to check if total_sum is divisible by 2 (even). If total_sum % 2 != 0, it means it's odd, so return False.
2fill in blank
mediumComplete the code to initialize the target sum for subset.
DSA Python
target = total_sum [1] 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' which returns float instead of integer.
Using '+' or '-' which are incorrect operations here.
✗ Incorrect
We use floor division '//' to get the integer half of total_sum as the target sum for the subset.
3fill in blank
hardFix the error in the loop to iterate over nums correctly.
DSA Python
for num in [1]: for i in range(target, num - 1, -1): dp[i] = dp[i] or dp[i - num]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'range(nums)' which is invalid because nums is a list.
Using 'target' or 'dp' which are not iterable lists of numbers.
✗ Incorrect
We need to iterate over the list 'nums' directly to access each number.
4fill in blank
hardFill both blanks to initialize the dp array and set the base case.
DSA Python
dp = [[1]] * (target + 1) dp[[2]] = True
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to False which breaks the base case.
Initializing dp with True which is incorrect.
✗ Incorrect
We initialize dp with False values and set dp[0] = True because sum 0 is always possible with empty subset.
5fill in blank
hardFill all three blanks to complete the function that returns if partition is possible.
DSA Python
def canPartition(nums): total_sum = sum(nums) if total_sum [1] 2 != 0: return False target = total_sum [2] 2 dp = [False] * (target + 1) dp[0] = True for num in nums: for i in range(target, num - 1, -1): dp[i] = dp[i] or dp[i - num] return dp[[3]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '//' for integer division.
Returning dp[0] instead of dp[target].
Using '==' instead of '%' for odd check.
✗ Incorrect
We check if total_sum % 2 != 0 to return False, use floor division '//' to get target, and return dp[target] to check if subset sum is possible.
