Find the Maximum Length of Valid Subsequence II - Problem
Find the Maximum Length of Valid Subsequence II

You are given an integer array nums and a positive integer k. Your task is to find the longest valid subsequence from the array.

A subsequence sub of length x is considered valid if all consecutive pairs in the subsequence have the same remainder when their sum is divided by k. In other words:

(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x-2] + sub[x-1]) % k

For example, if nums = [1, 2, 3, 4, 5] and k = 3, the subsequence [1, 2, 4] is valid because (1+2)%3 = 0 and (2+4)%3 = 0.

Goal: Return the length of the longest valid subsequence.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,3,4,5], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: The longest valid subsequence is [1,2,3,4]. Here (1+2)%3=0, (2+3)%3=2, but we can find [2,3,5,2] where all consecutive sums have remainder 2 when divided by 3, giving us length 4.
example_2.py โ€” All Same Remainder
$ Input: nums = [1,4,2,3,1,5], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: We can form subsequence [1,2,1,5] where all elements have remainder 1 or 2 mod 3, and consecutive sums maintain the same remainder pattern.
example_3.py โ€” Small k
$ Input: nums = [1,2], k = 2
โ€บ Output: 2
๐Ÿ’ก Note: The entire array [1,2] forms a valid subsequence since (1+2)%2=1, and there's only one consecutive pair.

Constraints

  • 1 โ‰ค nums.length โ‰ค 103
  • 1 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค k โ‰ค 103
  • A subsequence maintains the relative order of elements from the original array

Visualization

Tap to expand
๐ŸŽต Musical Harmony Chain BuilderInput: Notes [1, 2, 3, 4, 5] with Harmony Base k = 3Note Classes (mod 3): [1, 2, 0, 1, 2]๐ŸŽฏ Target Harmony: Remainder 01Class 1+2Class 2+3Class 0+4Class 1(1+2)%3=0 โœ“(2+3)%3=2 โœ—(3+4)%3=1 โœ—๐Ÿ“Š DP State EvolutionClass 0dp[0] = 3Class 1dp[1] = 2Class 2dp[2] = 3Maximum Chain Length3๐ŸŽฏ Result: Longest harmony chain has 3 notesBest sequence maintains consistent harmonic intervals across all consecutive pairs
Understanding the Visualization
1
Choose Target Harmony
Pick a target remainder (0 to k-1) that represents the harmonic interval we want
2
Track Note Classes
For each remainder class, track the longest harmony chain ending with that class
3
Extend Chains
For each new note, find which previous note class creates our target harmony, then extend that chain
4
Update Maximum
Keep track of the longest harmony chain found across all target harmonies
Key Takeaway
๐ŸŽฏ Key Insight: By treating each remainder class as a note type and using dynamic programming to track the longest harmony chains, we can efficiently build the optimal musical sequence with consistent harmonic intervals.
Asked in
Google 42 Meta 35 Amazon 28 Microsoft 22
38.2K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen