Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total sum of the array.
DSA Go
func canPartition(nums []int) bool {
total := 0
for _, num := range nums {
total += [1]
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole array 'nums' instead of the current element.
Adding zero instead of the current number.
✗ Incorrect
We add each element 'num' from the array 'nums' to the total sum.
2fill in blank
mediumComplete the code to check if the total sum is odd, return false immediately.
DSA Go
func canPartition(nums []int) bool {
total := 0
for _, num := range nums {
total += num
}
if total [1] 2 != 0 {
return false
}
return true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using division '/' instead of modulus '%'.
Using equality '==' instead of modulus.
✗ Incorrect
We use the modulus operator '%' to check if total is odd by checking remainder when divided by 2.
3fill in blank
hardFix the error in the code to initialize the dp slice with correct size.
DSA Go
func canPartition(nums []int) bool {
total := 0
for _, num := range nums {
total += num
}
if total%2 != 0 {
return false
}
target := total / 2
dp := make([]bool, [1])
dp[0] = true
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using total instead of target for dp size.
Using target without adding 1, causing index errors.
✗ Incorrect
We create a boolean slice dp of size target+1 to represent sums from 0 to target.
4fill in blank
hardFill both blanks to update dp slice correctly for each number.
DSA Go
for _, num := range nums { for j := [1]; j >= num; j-- { dp[j] = dp[j] || dp[j [2] num] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping forward instead of backward causing incorrect dp updates.
Using addition instead of subtraction inside dp index.
✗ Incorrect
We iterate backward from target down to num, and update dp[j] by checking dp[j] or dp[j - num].
5fill in blank
hardFill all three blanks to complete the function returning the correct result.
DSA Go
func canPartition(nums []int) bool {
total := 0
for _, num := range nums {
total += num
}
if total%2 != 0 {
return false
}
target := total / 2
dp := make([]bool, [1])
dp[0] = true
for _, num := range nums {
for j := [2]; j >= num; j-- {
dp[j] = dp[j] || dp[j [3] num]
}
}
return dp[target]
}Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect dp slice size causing index errors.
Looping forward instead of backward.
Using addition instead of subtraction inside dp index.
✗ Incorrect
The dp slice size is target+1, loop from target down to num, and update dp[j] using dp[j - num].
