Dynamic Programming: Knapsack - Equal Partition (Partition Equal Subset Sum)
In the following Python code snippet for the Equal Partition problem, which line introduces a logical error that can cause incorrect results?
def canPartition(nums):
total = sum(nums)
if total % 2 != 0:
return False
target = total // 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[target]