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

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2025-07-08Added By Anonymous
Source: Link

Company: Google
Role: unknown
Interview Stage: unknown
Location: unknown
Year: unknown

Original Post Content:

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(c= 3 || c= 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 c 0; consecutiveOdd < 3; ++consecutiveOdd)
for(int c 0; consecutiveEven < 3; ++consecutiveEven)
dp[n][consecutiveOdd][consecutiveEven] = 1;

for(int i = n-1; i >= 0; --i) {
for(int c 2; consecutiveOdd >= 0; --consecutiveOdd) {
for(int c 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 c 0; consecutiveOdd < 3; ++consecutiveOdd)
for(int c 0; consecutiveEven < 3; ++consecutiveEven)
nextRow[consecutiveOdd][consecutiveEven] = 1;

for(int i = n-1; i >= 0; --i) {
for(int c 2; consecutiveOdd >= 0; --consecutiveOdd) {
for(int c 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.

UPVOTE IF YOU LIKE THE SOLUTION 👍

Comments