Find the Maximum Length of Valid Subsequence II

You are given an integer array 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

Return the length of the longest valid subsequence of nums.

Example 1
Inputnums = [1,2,3,4,5], k = 2
Output5
The longest valid subsequence is [1, 2, 3, 4, 5].
Example 2
Inputnums = [1,4,2,3,1,4], k = 3
Output4
The longest valid subsequence is [1, 4, 1, 4].

Constraints

  • 2 <= nums.length <= 10^3
  • 1 <= nums[i] <= 10^7
  • 1 <= k <= 10^3

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate