Find the Maximum Length of Valid Subsequence I

You are given an integer array nums.

A subsequence sub of nums with length x is called valid if it satisfies:

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

Return the length of the longest valid subsequence of nums.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

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

Constraints

  • 2 <= nums.length <= 2 * 10^5
  • 1 <= nums[i] <= 10^7

Asked at 4 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