Google Recent | DP - Count Subsequences Variant | Top Down➡️Bottom Up | Clean CPP Code

2585
Jul 08, 2025
Jul 31, 2025

You are given an array nums[] of n integer elements. Determine the number of the non-empty subsequences of nums[] such that there do not exist three consecutive even or three consecutive odd integers in the subsequence. As the answer can be large, print it modulo 1e9 + 7.

Input: nums[] = {1, 5, 3}
Output: 6
Explanation: The only subsequeneces you can't take is {1, 5, 3} and {}.

Company Tags: Google
Constraints: nums.size() <= 10^5

Approach 1 : Using Top Down DP (Recursion To Memoization)

class TopDown {
    const int MOD = 1e9+7;
    int n;

    bool isOdd(int num) {
        return num % 2;
    }

    int solveWithMemo(vector<vector<vector<int>>>& dp, const vector<int>& nums, int i, int consecutiveOdd, int consecutiveEven) {
        if(consecutiveOdd == 3 || consecutiveEven == 3)
            return 0;
        
        if(i == n)
            return 1;

        if(dp[i][consecutiveOdd][consecutiveEven] != -1)
            return dp[i][consecutiveOdd][consecutiveEven];
        
        int currSkip = solveWithMemo(dp, nums, i + 1, consecutiveOdd, consecutiveEven);
        int currTake = solveWithMemo(dp, nums, i + 1, (isOdd(nums[i]) ? consecutiveOdd + 1 : 0), 
                                                      (!isOdd(nums[i]) ? consecutiveEven + 1 : 0));

        return dp[i][consecutiveOdd][consecutiveEven] = (currSkip + currTake) % MOD;
    }

public:
    // O(N) & O(N)
    int countSubsequences(vector<int>& nums) {
        n = nums.size();
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(3, vector<int>(3, -1)));
        return solveWithMemo(dp, nums, 0, 0, 0) - 1;
    }
};

Approach 2 : Using Bottom Up DP (3D + 2D Tabulation)

class BottomUp {
    const int MOD = 1e9+7;
    int n;

    bool isOdd(int num) {
        return num % 2;
    }

    // O(N) & O(N)
    int solveWith3DTable(const vector<int>& nums) {
        vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(4, vector<int>(4, 0)));

        for(int consecutiveOdd = 0; consecutiveOdd < 3; ++consecutiveOdd)
            for(int consecutiveEven = 0; consecutiveEven < 3; ++consecutiveEven)
                dp[n][consecutiveOdd][consecutiveEven] = 1;
        
        for(int i = n-1; i >= 0; --i) {
            for(int consecutiveOdd = 2; consecutiveOdd >= 0; --consecutiveOdd) {
                for(int consecutiveEven = 2; consecutiveEven >= 0; --consecutiveEven) {
                    int currSkip = dp[i + 1][consecutiveOdd][consecutiveEven];
                    int currTake = dp[i + 1][isOdd(nums[i]) ? consecutiveOdd + 1 : 0]
                                            [!isOdd(nums[i]) ? consecutiveEven + 1 : 0];
                    dp[i][consecutiveOdd][consecutiveEven] = (currSkip + currTake) % MOD;
                }
            }
        }

        return dp[0][0][0] - 1;
    }

    // O(N) & O(1)
    int solveWith2DTable(const vector<int>& nums) {
        vector<vector<int>> nextRow(4, vector<int>(4, 0)), idealRow(4, vector<int>(4, 0));

        for(int consecutiveOdd = 0; consecutiveOdd < 3; ++consecutiveOdd)
            for(int consecutiveEven = 0; consecutiveEven < 3; ++consecutiveEven)
                nextRow[consecutiveOdd][consecutiveEven] = 1;
        
        for(int i = n-1; i >= 0; --i) {
            for(int consecutiveOdd = 2; consecutiveOdd >= 0; --consecutiveOdd) {
                for(int consecutiveEven = 2; consecutiveEven >= 0; --consecutiveEven) {
                    int currSkip = nextRow[consecutiveOdd][consecutiveEven];
                    int currTake = nextRow[isOdd(nums[i]) ? consecutiveOdd + 1 : 0]
                                          [!isOdd(nums[i]) ? consecutiveEven + 1 : 0];
                    idealRow[consecutiveOdd][consecutiveEven] = (currSkip + currTake) % MOD;
                }
            }
            nextRow = idealRow;
        }

        return nextRow[0][0] - 1;
    }

public:
    int countSubsequences(vector<int>& nums) {
        n = nums.size();
        return solveWith2DTable(nums);
    }
};

Note: Found this problem on someone’s LinkedIn post, it says this question was recently asked by Google. The constraints, company tags, and inputs provided in this are derived from there. However, due to LeetCode guidelines, I cannot link to the original post.
Link to the post is added in the comments.

𝗨𝗣𝗩𝗢𝗧𝗘 𝗜𝗙 𝗬𝗢𝗨 𝗟𝗜𝗞𝗘 𝗧𝗛𝗘 𝗦𝗢𝗟𝗨𝗧𝗜𝗢𝗡 👍

Comments (12)