Data Structures & AlgorithmsVery hardLeetCode-style · GATE CS

What is the minimum number of elements you must INSERT into [1, 4, 2, 7, 4, 1, 3] (at any positions) so that the array becomes a palindrome?

  1. A. 4
  2. B. 3
  3. C. 1
  4. D. 2

Correct answer: D. 2

Insertions needed = n − (length of the longest palindromic subsequence), because the elements left unmatched can be mirrored by insertions. Equivalently, dp[i][j] = dp[i+1][j−1] if a[i] = a[j], else 1 + min of the two shrinks.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic