DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `45`

πŸ”Ή Problem: 3201. Find the Maximum Length of Valid Subsequence I

Difficulty: #Medium
Tags: #Array, #Greedy


πŸ“ Problem Summary

You are given an array nums.
A subsequence is valid if all adjacent elements satisfy:
(a + b) % 2 == constant β€” i.e., all adjacent pairwise sums are either always even or always odd.
Return the length of the longest valid subsequence.


🧠 My Thought Process

  • Brute Force Idea:
    Try generating all possible subsequences and check if they meet the condition β€” obviously way too slow for n <= 2*10^5.

  • Optimized Strategy:
    The condition (a + b) % 2 == constant means the subsequence must either:

    • Always have same parity (odd+odd or even+even)
    • Or alternate between even and odd (odd+even or even+odd)

This means there are only 4 patterns to check:

  • [even, even, ...]
  • [odd, odd, ...]
  • [even, odd, even, ...]
  • [odd, even, odd, ...]

So, for each pattern, greedily scan the array and pick numbers that match the expected parity at that position.


βš™οΈ Code Implementation (Python)

class Solution:
    def maximumLength(self, nums: List[int]) -> int:
        res = 0
        for pattern in [[0, 0], [0, 1], [1, 0], [1, 1]]:
            cnt = 0
            for num in nums:
                if num % 2 == pattern[cnt % 2]:
                    cnt += 1
            res = max(res, cnt)
        return res
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(4 * n) β†’ O(n)
  • Space: O(1)

🧩 Key Takeaways

  • βœ… Identifying the parity pattern reduces the problem from an unclear DP to a greedy solution.
  • πŸ’‘ Only 4 valid parity sequences exist β€” just simulate them all.
  • πŸ’­ Sometimes, thinking in terms of mod 2 simplifies problems more than trying to force DP.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help? (almost!)
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week?

πŸ“š Related Problems

  • [[300 Longest Increasing Subsequence]]
  • [[2915 Length of the Longest Subsequence That Sums to Target]]

πŸš€ Progress Tracker

Metric Value
Day 45
Total Problems Solved 386
Confidence Today πŸ˜ƒ
Leetcode Rating 1572

Top comments (0)