Watch 10 video solutions for Find the Maximum Length of Valid Subsequence II, a medium level problem involving Array, Dynamic Programming. This walkthrough by codestorywithMIK has 14,456 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
nums and a positive integer k.
A subsequence sub of nums with length x is called valid if it satisfies:
(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.nums.
Example 1:
Input: nums = [1,2,3,4,5], k = 2
Output: 5
Explanation:
The longest valid subsequence is [1, 2, 3, 4, 5].
Example 2:
Input: nums = [1,4,2,3,1,4], k = 3
Output: 4
Explanation:
The longest valid subsequence is [1, 4, 1, 4].
Constraints:
2 <= nums.length <= 1031 <= nums[i] <= 1071 <= k <= 103Problem Overview: You are given an integer array and a value k. The goal is to build the longest subsequence such that every adjacent pair in the subsequence has the same remainder when their sum is taken modulo k. Order must follow the original array, so the problem becomes finding the longest valid chain of elements whose pairwise modulo condition stays constant.
Approach 1: Prefix Sum and Modulo Grouping (Dynamic Programming) (Time: O(n * k), Space: O(k))
The key observation is that the condition (a + b) % k = target can be rewritten using remainders. If a % k = r, then the next element must have remainder (target - r + k) % k. Iterate through the array and reduce each number to its remainder modulo k. For every possible target remainder 0..k-1, maintain a small DP structure that tracks the longest subsequence ending with each remainder. When you process a new element, compute the required previous remainder that would satisfy the pair condition and extend that subsequence by one. This effectively turns the problem into remainder transitions rather than raw value comparisons. The method works well because the modulo range is limited to k, making the DP compact. This approach directly uses ideas from dynamic programming and remainder grouping from array problems.
Approach 2: Frequency Map for Remainder Conditions (Time: O(n + k^2), Space: O(k))
Another way to view the problem is through remainder frequencies. Convert every value to num % k and count how often each remainder appears. For a fixed target value t, a valid adjacent pair must satisfy (r1 + r2) % k = t. This means remainder r1 pairs with (t - r1 + k) % k. Iterate through all possible target remainders and simulate building the longest alternating chain of compatible remainders using the frequency map. Each step extends the sequence by consuming counts of matching remainders. Because only k remainder classes exist, exploring all pair relationships costs O(k^2). This approach is easier to reason about when you focus on remainder compatibility rather than element positions and relies on constant-time lookups with a hash map or frequency array, a common pattern in hash map based solutions.
Recommended for interviews: The dynamic programming remainder-transition approach is what most interviewers expect. It shows you can transform a numeric constraint into a state transition over modulo classes. Mentioning the brute reasoning about pair remainders demonstrates understanding, but implementing the DP efficiently demonstrates stronger algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Prefix Sum and Modulo Grouping (DP) | O(n * k) | O(k) | General case when processing elements in order and extending subsequences dynamically |
| Frequency Map for Remainder Conditions | O(n + k^2) | O(k) | Useful when focusing on remainder compatibility and counts rather than element positions |