ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[6] = {5, 5, 10, 100, 10, 5}
Integer incl = 0, excl = 0, t
for (each i from 0 to 5)
t = max(incl, excl)
incl = excl + a[i]
excl = t
end for
Print max(incl, excl)- A. 135
- B. 110
- C. 115
- D. 105
Correct answer: B. 110
This DP finds the largest sum of elements with no two adjacent. The best choice is 5 + 100 + 5 = 110. Taking every other element from the start gives 5 + 10 + 10 = 25, and from the second element 5 + 100 + 5 = 110 as well.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- An array of n elements is ALREADY sorted in increasing order. What is the time complexity of this pseudocode on it? for (each i from 1 to…
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 7086351, s = 0, d…
- What will be the output of the following pseudocode? Procedure upd(Integer arr[], Integer n) // arrays are passed by reference arr[0] =…
- What will be the output of the following pseudocode? (& is bitwise AND) Integer x = 255, y = 256, c1 = 0, c2 = 0 while (x > 0) x = x & (x…
- What will be the output of the following pseudocode? Integer cnt = 0 Procedure go(Integer i, Integer s) if (s == 10) cnt = cnt + 1 return…