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]))- A. False False
- B. True False
- C. True True
- 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
- What is printed? python def paths(g): R, C = len(g), len(g[0]) dp = [[0] C for in range(R)] dp[0][0] = 0 if g[0][0] else 1 for r in…
- What is printed? python def cool(p): hold, sold, rest = float('-inf'), 0, 0 for x in p: hold, sold, rest = max(hold, rest - x), hold + x…
- What is printed? python import bisect def inv(a): s, c = [], 0 for x in a: c += len(s) - bisect.bisectright(s, x) bisect.insort(s, x)…
- What is printed? python import bisect a = [10, 20, 20, 30] print([bisect.bisectleft(a, x) for x in (20, 25, 5, 40)])
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def grid(r, c): if r == 0 or c == 0: return 1 return grid(r…
- What is printed? python def cancomplete(gas, cost): total = tank = start = 0 for i in range(len(gas)): d = gas[i] - cost[i] total += d…