GATE Computer ScienceVery hardGATE CS (PYQ pattern)
In how many ways can 12 be written as an ordered sum of parts from {1, 2, 3} such that no two adjacent parts are equal (e.g. 1+2+1+3+… is allowed, 2+2+… is not)?
- A. 58
- B. 927
- C. 64
- D. 49
Correct answer: A. 58
Use a DP f(r, last) = number of ways to finish a remaining total r when the previous part was last: f(0, ·) = 1, f(r, last) = Σ f(r − p, p) over p ∈ {1, 2, 3}, p ≠ last, p ≤ r. Evaluating gives f(12, none) = 58. Without the adjacency rule there are 927 compositions.
More Algorithms questions
- Which of the following statements about dynamic programming problems is/are correct? I. The LCS of two strings can be computed in O(mn)…
- Ten houses stand in a CIRCLE with values 6, 2, 9, 4, 3, 8, 1, 7, 5, 10 (the first and last houses are neighbours). No two neighbouring…
- Among all full parenthesisations of the product A₁A₂A₃A₄A₅A₆A₇, how many contain the product (A₃A₄) as one of their sub-products?
- X = ABCBDAB and Y = BDCABA have longest common subsequences of length 4. How many DIFFERENT strings are shortest common supersequences of…
- The fractional knapsack greedy algorithm (taking items by value/weight ratio, the last one fractionally) is run on capacity 26 and items…
- Dijkstra's algorithm (never re-inserting or re-opening a vertex once it has been extracted) is run from S on a directed graph with edges…