ProgrammingHardGATE CS & IT · Infosys

What is printed?

What is printed?
def can(a):
    s = sum(a)
    if s % 2:
        return False
    t = s // 2
    dp = [False] * (t + 1)
    dp[0] = True
    for x in a:
        for v in range(t, x - 1, -1):
            dp[v] = dp[v] or dp[v - x]
    return dp[t]
print(can([1, 5, 11, 5]), can([1, 2, 3, 5]))
  1. A. False False
  2. B. True False
  3. C. True True
  4. D. False True

Correct answer: B. True False

The subset-sum DP looks for a sum of half the total. [1,5,11,5] splits into {11} and {1,5,5}, while [1,2,3,5] has an odd total of 11 and cannot be split.

More Python Programming questions

Exams that ask this topic