Data Structures & AlgorithmsHardCodeforces Div2 D · TCS NQT
In [3, 4, 9, 5, 6, 2, 1, 7, 8], what is the length of the longest subsequence (keeping the order) in which every pair of consecutive chosen values differs by exactly 1?
- A. 6
- B. 2
- C. 9
- D. 3
Correct answer: A. 6
dp[i] = 1 + max dp[j] over earlier j with |a[i] − a[j]| = 1; 3, 4, 5, 6, 7, 8 gives 6. Requiring the chosen values to be adjacent in the array gives only 2, 9 is the array length, and 3 is a short chain such as 3, 4, 5.
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
- What is the minimum number of pieces needed to split "abacdcbaxyzyx" into contiguous pieces where every piece is a palindrome?
- You may increase any element of [5, 3, 2, 10, 4] by any amount (increases only). What is the minimum total increase needed to make the…
- A path of length 9 is walked from 0 to 9: a step of 1 costs 3 and a step of 2 costs 5. What is the minimum total cost?
- A 1 × 11 strip must be covered exactly by tiles of lengths 2, 3 and 5 (tiles cannot overlap or stick out). Tile lengths 2, 3, 5 cost 4, 5…
- What is the length of the longest CONTIGUOUS block that appears in both arrays [1, 5, 3, 7, 2, 3, 7, 8] and [4, 7, 2, 3, 7, 9, 1] (equal…
- What is the minimum number of letters you must insert (into either string) so that "leetcode" and "codeleet" become anagrams of each other?