Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Length of Valid Subsequence II

Find the Maximum Length of Valid Subsequence II gives an integer array and positive k. Return the longest subsequence whose adjacent pairs all produce the same remainder when their sum is divided by k.

Statement

Given an integer array nums and a positive integer k.

A subsequence sub of nums with length xx is defined as valid if the sum of every adjacent pair of elements modulo k is equal. That is:

(‘sub[0]‘+‘sub[1]‘)(mod‘k‘)=(‘sub[1]‘+‘sub[2]‘)(mod‘k‘)=⋯=(‘sub[x−2]‘+‘sub[x−1]‘)(mod‘k‘)(`sub[0]` + `sub[1]`) \pmod{`k`} = (`sub[1]` + `sub[2]`) \pmod{`k`} = \dots = (`sub[x - 2]` + `sub[x - 1]`) \pmod{`k`}

Return the maximum length of a valid subsequence of nums.

Note: A subsequence is derived from an array by deleting zero or more elements without changing the order of the remaining elements.

Constraints:

  • 2≤2 \leq nums.length ≤103\leq 10^3

  • 1≤1 \leq nums[i] ≤107\leq 10^7

  • 1≤1 \leq k ≤103\leq 10^3

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Length of Valid Subsequence II

Find the Maximum Length of Valid Subsequence II gives an integer array and positive k. Return the longest subsequence whose adjacent pairs all produce the same remainder when their sum is divided by k.

Statement

Given an integer array nums and a positive integer k.

A subsequence sub of nums with length xx is defined as valid if the sum of every adjacent pair of elements modulo k is equal. That is:

(‘sub[0]‘+‘sub[1]‘)(mod‘k‘)=(‘sub[1]‘+‘sub[2]‘)(mod‘k‘)=⋯=(‘sub[x−2]‘+‘sub[x−1]‘)(mod‘k‘)(`sub[0]` + `sub[1]`) \pmod{`k`} = (`sub[1]` + `sub[2]`) \pmod{`k`} = \dots = (`sub[x - 2]` + `sub[x - 1]`) \pmod{`k`}

Return the maximum length of a valid subsequence of nums.

Note: A subsequence is derived from an array by deleting zero or more elements without changing the order of the remaining elements.

Constraints:

  • 2≤2 \leq nums.length ≤103\leq 10^3

  • 1≤1 \leq nums[i] ≤107\leq 10^7

  • 1≤1 \leq k ≤103\leq 10^3