Skip to content

Practice Find the Maximum Length of Valid Subsequence I

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3201
LeetCode Problem

Find the Maximum Length of Valid Subsequence I

Problem Statement

Given nums, find the longest subsequence for which all sums of neighboring selected elements have the same parity. Equivalently, (sub[i] + sub[i + 1]) % 2 must have one common value across all adjacent pairs. Preserve the original order of selected elements and return the maximum length.

Source: Find the Maximum Length of Valid Subsequence I on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,3,4]
Output
4

# Example 2

Input
nums = [1,2,1,1,2,1,2]
Output
6

# Example 3

Input
nums = [1,3]
Output
2

Constraints

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

Related LeetCode Problems

Practice more problems similar to Find the Maximum Length of Valid Subsequence I.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.