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?
- A. 4
- B. 3
- C. 1
- 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.
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
- How many contiguous subarrays of length at least 2 of [23, 2, 4, 6, 7] have a sum that is a multiple of 6?
- Sum the lengths of all contiguous subarrays of [1, 2, 3, 4] whose element sum is even.
- How many index pairs i < j in [1, 4, 6, 9, 10, 12] satisfy a[i] + a[j] >= 15?
- How many subarrays [i..j] of [3, 1, 4, 1, 5] have their first element strictly greater than every other element of the subarray?
- What is the smallest positive integer x such that x(x+1)(x+2)/6 is at least 100?
- What is the maximum number of pairwise disjoint non-empty contiguous subarrays of [1, -1, 2, -2, 3, -3, 4] that each have sum zero?