Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Math
Google
Find The Maximum Length Of Valid Subsequence Ii

Find the longest subsequence whose adjacent elements satisfy a given modular validity rule.

Acceptance 100%
Problem Statement

Problem

Given an integer array, choose a subsequence (not necessarily contiguous) with maximum possible length such that the subsequence is valid according to the problem's rule.

For this problem family, the validity condition depends on the values of adjacent chosen elements and typically involves a modular or parity-based relationship. Your task is to return the maximum length of any valid subsequence.

A subsequence is formed by deleting zero or more elements without changing the order of the remaining elements.

Input Format

  • An integer array nums.
  • The exact validity rule is determined by the problem's definition and must be respected when forming the subsequence.

Output Format

  • Return a single integer: the maximum length of a valid subsequence.

Constraints

  • 1 <= nums.length.
  • nums[i] are integers.
  • The answer fits in a 32-bit signed integer.
  • Exact numeric bounds are not provided in the source metadata, so treat the array as medium-sized interview input.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3, 4]

Output

4

Explanation

This is an illustrative example: if all adjacent chosen pairs satisfy the validity rule, the entire array can be taken as the subsequence.

Example 2

Input

nums = [5, 1, 4, 2, 3]

Output

3

Explanation

This is an illustrative example: one valid subsequence may be longer than many obvious greedy picks, so dynamic programming over states is useful.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.