CS Atlas GATE 2027
Algorithms course
0 / 79 complete

Algorithms/Phase 5 · Dynamic Programming

Chapter review LI-5.2 · Item 58 of 79

More DP problems

Read at your own pace. Try the questions before opening the answers.

On this page

ACompressed recap

🧾

Matrix chain: m[i][j] = min_k m[i][k] + m[k+1][j] + p_{i−1}p_k p_j; fill by length; Θ(n³); C_{n−1} parenthesisations. Subset sum R[x] |= R[x − s] (downward), Θ(nt). Coin ways: coins outer, W[x] += W[x − c]. Min coins: 1 + min D[x − c]. Rod cutting: max pᵢ + R[j − i]. LIS: best ending at i, Θ(n²). Edit distance: min(delete, insert, substitute), borders i and j, Θ(mn). Catalan: BSTs and parenthesisations.

BMerged rule card

5.2.1 Matrix-chain multiplication

  • m[i][i] = 0; m[i][j] = min_k (m[i][k] + m[k+1][j] + p_{i−1}p_k p_j).
  • Fill by chain length; s[i][j] stores the best k.
  • Θ(n³) time, Θ(n²) space; parenthesisations C_{n−1}.

5.2.2 Subset sum, coin change and rod cutting

  • Subset sum: R[x] |= R[x − s], x downward; Θ(n·t). Counting: +=.
  • Coin ways: coins outer, amounts upward: W[x] += W[x − c].
  • Min coins: D[x] = 1 + min D[x − c].
  • Rod cutting: R[j] = max(pᵢ + R[j − i]); Θ(n²).

5.2.3 Longest increasing subsequence, edit distance and other sequence DPs

  • LIS: L[i] = 1 + max L[j] (j < i, A[j] < A[i]); answer max L[i]; Θ(n²) (n log n possible).
  • Edit distance: min(delete, insert, substitute/match); borders i and j; Θ(mn).
  • BSTs on n keys: Catalan Cₙ.

CInterleaved GATE-style questions

Q1NAT2 marksMatrix chain

Dimensions p = (5, 10, 3, 12, 5, 50, 6). Minimum scalar multiplications?

Numerical answer: ________

Q2NAT2 marksMatrix chain

Dimensions p = (10, 100, 5, 50). Minimum scalar multiplications?

Numerical answer: ________

Q3NAT2 marksCoin ways

Ways to make 4 with coins {1, 2, 3} (order ignored)?

Numerical answer: ________

Q4NAT2 marksRod

Rod prices (lengths 1–5): 2, 5, 7, 8, 10. Maximum revenue for length 5?

Numerical answer: ________

Q5NAT2 marksSubset

Number of subsets of {1, 2, 3, 4, 5} summing to 10?

Numerical answer: ________

Q6NAT2 marksEdit

Edit distance between abc and yabd?

Numerical answer: ________

Q7NAT1 markLIS

LIS length of [10, 9, 2, 5, 3, 7, 101, 18]?

Numerical answer: ________

Q8MCQ1 markLoop order

In the coin-change "number of ways" DP, putting the amount loop outside and the coin loop inside counts:

  • (A) ordered sequences (order matters)
  • (B) combinations (order ignored)
  • (C) the minimum number of coins
  • (D) nothing meaningful

DAnswer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Q1 Answer: 20102 marks

Best order ((A₁A₂)((A₃A₄)(A₅A₆))).

Re-read: Lesson 5.2.1 — Matrix-chain multiplication

Q2 Answer: 75002 marks

(A₁A₂)A₃.

Re-read: Lesson 5.2.1 — Matrix-chain multiplication

Q3 Answer: 42 marks

1+1+1+1, 1+1+2, 2+2, 1+3.

Re-read: Lesson 5.2.2 — Subset sum, coin change and rod cutting

Q4 Answer: 122 marks

2 + 2 + 1: 5 + 5 + 2.

Re-read: Lesson 5.2.2 — Subset sum, coin change and rod cutting

Q5 Answer: 32 marks

{1, 4, 5}, {2, 3, 5}, {1, 2, 3, 4}.

Re-read: Lesson 5.2.2 — Subset sum, coin change and rod cutting

Q6 Answer: 22 marks

Insert y, substitute c → d.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q7 Answer: 41 mark

2, 5, 7, 101.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q8 Answer: (A)1 mark

Each amount extends every sequence by any coin.

  • (B) is wrong: That needs coins outside.
  • (C) is wrong: Different recurrence.
  • (D) is wrong: It counts compositions.

Re-read: Lesson 5.2.2 — Subset sum, coin change and rod cutting

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.