ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting combinations, not permutations)?
What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting combinations, not permutations)?
Integer coins[3] = {1, 2, 5}
Integer amount = 5
Integer dp[6] = all zeros
dp[0] = 1
for (each c in coins)
for (each a from c to amount)
dp[a] = dp[a] + dp[a - c]
end for
end for
Print dp[amount]- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
The 4 ways to make 5 using {1,2,5} as combinations are: 1+1+1+1+1, 1+1+1+2, 1+2+2, and 5 itself.
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 (length of the longest common SUBSTRING, i.e. contiguous, not subsequence)? Function…
- What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are…
- What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)? String s = "hello world foo"…
- What will be the output of the following pseudocode (maximum sum of any 4 consecutive elements, sliding window)? (Arrays are indexed from…
- What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.) Integer arr[8]…
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…