The Indian Engineer

Problem 3201 Find the Maximum Length of Valid Subsequence I

Posted on 5 mins

Array Dynamic-Programming Dp

Problem Statement

Link - Problem 3201

Question

You are given an integer array nums.

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

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:

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

Output: 4

Explanation:

The longest valid subsequence is [1, 2, 3, 4].

Example 2:

Input: nums = [1,2,1,1,2,1,2]

Output: 6

Explanation:

The longest valid subsequence is [1, 2, 1, 2, 1, 2].

Example 3:

Input: nums = [1,3]

Output: 2

Explanation:

The longest valid subsequence is [1, 3].

Constraints:

Solution

class Solution {
public:
    int maximumLength(vector<int>& nums) {
        int type_1 = 0;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i]%2==0){
                type_1++;
            }
        }
        int type_2=0;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i]%2==1){
                type_2++;
            }
        }
        bool turn = true;
        int type_3 = 0;
        for(int i = 0; i < nums.size(); i++){
            if(turn && nums[i]%2==0){
                turn=false;
            } else if(!turn && nums[i]%2==1){
                turn=true;
                type_3+=2;
            }
        }
        if(turn==false){
            type_3++;
        }
        turn = true;
        int type_4=0;
        for(int i = 0; i < nums.size(); i++){
            if(turn && nums[i]%2==1){
                turn=false;
            } else if(!turn && nums[i]%2==0){
                type_4+=2;
                turn=true;
            }
        }
        if(turn==false){
            type_4++;
        }
        return max(max(type_1, type_2),max(type_3, type_4));
    }
};

Complexity Analysis

| Algorithm                 | Time Complexity | Space Complexity |
| ------------------------- | --------------- | ---------------- |
| Simple Frequency Counting | O(n)            | O(1)             |

Explanation

Intial Thoughts

This problem can be approached by first identifying the pattern required for a valid subsequence, where each pair of consecutive numbers sums up to either an even or odd number consistently throughout the subsequence. We can consider the numbers to be of two types: even and odd. We need to maximize the length of the valid subsequence, so we should consider all possible scenarios for achieving this. Looking at the solution, we find four different types of subsequences being considered. The first two types involve only odd or even numbers. However, the last two types involve alternating between odd and even numbers. A valid subsequence could end with either an even or odd number depending on the starting number and the type of subsequence we are dealing with.

Intuitive Analysis

The provided solution has correctly analyzed all possible types of valid subsequences. It first counts the number of odd and even numbers separately, and then considers the two alternating scenarios. In each scenario, it pairs the numbers from the start and the end, working its way towards the center to maximize the subsequence length. At the end, it chooses the maximum length from the four types of subsequences. We can intuitively understand that this is the best approach because we have considered all possible arrangements and are choosing the one that gives the maximum length.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

The possible sequence either contains all even elements, all odd elements, alternate even odd, or alternate odd even elements.

Considering only the parity of elements, there are only 4 possibilities and we can try all of them.

When selecting an element with any parity, try to select the earliest one.


Similar Questions:

Title URL Difficulty
Longest Increasing Subsequence https://leetcode.com/problems/longest-increasing-subsequence Medium
Length of the Longest Subsequence That Sums to Target https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target Medium