Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 D
Counting ways to pay each amount 0..9 with coins [1, 4, 6]: version A loops over amounts (outer) and coins (inner), counting ordered sequences of coins. Version B loops over coins (outer) and amounts (inner), counting unordered selections. Both start with d[0] = 1. At which amount index do the two tables first differ? Answer −1 if they agree everywhere.
- A. 5
- B. 6
- C. 4
- D. 7
Correct answer: A. 5
Only the ordered version counts 1 + 4 and 4 + 1 as two ways. With coins as the outer loop each coin type is processed once, so the first amount that can be paid in two different orders is where the tables first disagree.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For the array [4, 2, 5, 3, 1, 6, 0], a 'next greater element' routine returns for each index the value of the next strictly larger element…
- A routine builds a list t: for each x in a, it appends x to t only if x is larger than the last element of t (otherwise it skips x), and…
- Items as (weight, value) are [(2, 3), (3, 4), (1, 1)]. The 0/1 knapsack table dp[c] for capacities c = 0..6 is built correctly by scanning…
- Intervals [(1, 6), (2, 4), (3, 9), (5, 7), (8, 10), (4, 5)] are given as closed ranges. You must remove the fewest intervals so that no…
- How many non-empty contiguous subarrays of [1, -1, 2, 0, 1, 3, -2, 1] have a sum equal to their length (number of elements)? Transform…
- Choose any subset of the positions of [4, -2, 7, 3, 9, 1, -5, 8, 2] (possibly empty) so that every two chosen positions are at least 3…