ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Function canSplit(Integer a[], Integer n)
Integer total = sum of a
if (total mod 2 == 1) return false
Boolean ok[0..total / 2] = all false
ok[0] = true
for (each i from 0 to n - 1)
for (each s from total / 2 down to a[i])
ok[s] = ok[s] OR ok[s - a[i]]
end for
end for
return ok[total / 2]
End Function
Print canSplit({1, 5, 11, 5}, 4), canSplit({1, 2, 3, 5}, 4), canSplit({2, 2, 3, 5}, 4)- A. true false true
- B. true false false
- C. true true false
- D. false false false
Correct answer: B. true false false
The subset-sum table checks whether half the total can be reached. {1, 5, 11, 5}: 11 = 1 + 5 + 5, true. {1, 2, 3, 5}: total 11 is odd, false. {2, 2, 3, 5}: half is 6, but no subset sums to 6 (2+2 = 4, 2+3 = 5, 2+2+3 = 7…), false. Output true false false.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Function rob(Integer a[], Integer lo, Integer hi) Integer inc = 0, exc = 0, t for…
- What will be the output of the following pseudocode? Integer cost[3] = {10, 15, 20} Integer a = 0, b = 0, c // cheapest cost to stand on…
- What will be the output of the following pseudocode? Pair iv[11] = {(1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12)…
- What will be the output of the following pseudocode? Integer arr[6] = {900, 940, 950, 1100, 1500, 1800} Integer dep[6] = {910, 1200, 1120…
- What will be the output of the following pseudocode? (& is AND, >> is right shift) Integer x = 40, pos = 1 while ((x & 1) == 0) x = x >> 1…