Count of all subsequences having adjacent elements with different parity

Given an array arr[] of size N, the task is to find the number of non-empty subsequences from the given array such that no two adjacent element of the subsequence have same parity.
Examples: 
 

Input: arr[] = [5, 6, 9, 7] 
Output:
Explanation: 
All such subsequences of given array will be {5}, {6}, {9}, {7}, {5, 6}, {6, 7}, {6, 9}, {5, 6, 9}, {5, 6, 7}.
Input: arr[] = [2, 3, 4, 8] 
Output:
 

 

Naive Approach: Generate all non-empty subsequences and select the ones with alternate odd-even or even-odd numbers and count all such subsequences to obtain the answer. 
Time Complexity: O(2N)
Efficient Approach: 
The above approach can be optimized using Dynamic Programming. Follow the steps below to solve the problem: 
 

  • Consider a dp[] matrix of dimensions (N+1)*(2).
  • dp[i][0] stores the count of subsequences till ith index ending with an even element.
  • dp[i][1] stores the count of subsequences till ith index ending with an odd element.
  • Hence, for every ith element, check if the element is even or odd and proceed by including as well as excluding the ith element.
  • Hence, the recurrence relation if the ith element is odd: 
     

 
 



dp[i][1] = dp[i – 1][0] (Including the ith element by considering all subsequences ending with even element till (i – 1)th index) + 1 + dp[i – 1][1] (Excluding the ith element) 
 

 

  • Similarly, if the ith element is even: 
     

 
 

dp[i][0] = dp[i – 1][1] (Including the ith element by considering all subsequences ending with odd element till (i – 1)th index) + 1 + dp[i – 1][0] (Excluding the ith element) 
 

 

  • Finally, the sum of dp[n][0], which contains all such subsequences ending with an even element, and dp[n][1], which contains all such subsequences ending with an odd element, is the required answer.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement the 
// above approach 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to find required subsequences 
int validsubsequences(int arr[], int n) 
    // dp[i][0]: Stores the number of 
    // subsequences till i-th index 
    // ending with even element 
    // dp[i][1]: Stores the number of 
    // subsequences till i-th index 
    // ending with odd element 
    long long int dp[n + 1][2]; 
  
    // Initialise the dp[][] with 0. 
    for (int i = 0; i < n + 1; i++) { 
        dp[i][0] = 0; 
        dp[i][1] = 0; 
    
  
    for (int i = 1; i <= n; i++) { 
  
        // If odd element is 
        // encountered 
        if (arr[i - 1] % 2) { 
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i][1] += 1; 
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with even element 
            // till (i-1)th indexes 
            dp[i][1] += dp[i - 1][0]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i][1] += dp[i - 1][1]; 
  
            dp[i][0] += dp[i - 1][0]; 
        
        else
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i][0] += 1; 
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with odd element 
            // till (i-1)th indexes 
            dp[i][0] += dp[i - 1][1]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i][0] += dp[i - 1][0]; 
            dp[i][1] += dp[i - 1][1]; 
        
    
  
    // Count of all valid subsequences 
    return dp[n][0] + dp[n][1]; 
  
// Driver Code 
int main() 
    int arr[] = { 5, 6, 9, 7 }; 
    int n = sizeof(arr) / sizeof(arr[0]); 
  
    cout << validsubsequences(arr, n); 
  
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program implementation
// of the approach
import java.util.*;
import java.io.*;
  
class GFG{
  
// Function to find required subsequences     
static int validsubsequences(int arr[], int n)
{
      
    // dp[i][0]: Stores the number of 
    // subsequences till i-th index 
    // ending with even element 
    // dp[i][1]: Stores the number of 
    // subsequences till i-th index 
    // ending with odd element 
    long dp[][] = new long [n + 1][2]; 
  
    // Initialise the dp[][] with 0. 
    for(int i = 0; i < n + 1; i++)
    
        dp[i][0] = 0
        dp[i][1] = 0
    }
      
    for(int i = 1; i <= n; i++)
    
          
        // If odd element is 
        // encountered 
        if (arr[i - 1] % 2 != 0
        
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i][1] += 1
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with even element 
            // till (i-1)th indexes 
            dp[i][1] += dp[i - 1][0]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i][1] += dp[i - 1][1]; 
            dp[i][0] += dp[i - 1][0]; 
        
        else
        
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i][0] += 1
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with odd element 
            // till (i-1)th indexes 
            dp[i][0] += dp[i - 1][1]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i][0] += dp[i - 1][0]; 
            dp[i][1] += dp[i - 1][1]; 
        
    
  
    // Count of all valid subsequences 
    return (int)(dp[n][0] + dp[n][1]); 
}
  
// Driver code 
public static void main(String[] args) 
{
    int arr[] = { 5, 6, 9, 7 };
    int n = arr.length;
          
    System.out.print(validsubsequences(arr, n));
}
}
  
// This code is contributed by code_hunt

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement the
# above approach
  
# Function to find required subsequences
def validsubsequences(arr, n):
  
    # dp[i][0]: Stores the number of
    # subsequences till i-th index
    # ending with even element
    # dp[i][1]: Stores the number of
    # subsequences till i-th index
    # ending with odd element
  
    # Initialise the dp[][] with 0.
    dp = [[0 for i in range(2)]
             for j in range(n + 1)]
               
    for i in range(1, n + 1):
  
        # If odd element is
        # encountered
        if(arr[i - 1] % 2):
  
            # Considering i-th element
            # will be present in
            # the subsequence
            dp[i][1] += 1
  
            # Appending i-th element to all
            # non-empty subsequences
            # ending with even element
            # till (i-1)th indexes
            dp[i][1] += dp[i - 1][0]
  
            # Considering ith element will
            # not be present in
            # the subsequence
            dp[i][1] += dp[i - 1][1]
            dp[i][0] += dp[i - 1][0]
  
        else:
  
            # Considering i-th element
            # will be present in
            # the subsequence
            dp[i][0] += 1
  
            # Appending i-th element to all
            # non-empty subsequences
            # ending with odd element
            # till (i-1)th indexes
            dp[i][0] += dp[i - 1][1]
  
            # Considering ith element will
            # not be present in
            # the subsequence
            dp[i][0] += dp[i - 1][0]
            dp[i][1] += dp[i - 1][1]
  
    # Count of all valid subsequences 
    return dp[n][0] + dp[n][1]
  
# Driver code
if __name__ == '__main__':
  
    arr = [ 5, 6, 9, 7 ]
    n = len(arr)
  
    print(validsubsequences(arr, n))
  
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program implementation
// of the approach
using System;
  
class GFG{
  
// Function to find required subsequences     
static int validsubsequences(int[] arr, int n)
{
      
    // dp[i][0]: Stores the number of 
    // subsequences till i-th index 
    // ending with even element 
    // dp[i][1]: Stores the number of 
    // subsequences till i-th index 
    // ending with odd element 
    long[,] dp = new long [n + 1, 2]; 
  
    // Initialise the dp[][] with 0. 
    for(int i = 0; i < n + 1; i++)
    
        dp[i, 0] = 0; 
        dp[i, 1] = 0; 
    }
      
    for(int i = 1; i <= n; i++)
    
          
        // If odd element is 
        // encountered 
        if (arr[i - 1] % 2 != 0) 
        
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i, 1] += 1; 
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with even element 
            // till (i-1)th indexes 
            dp[i, 1] += dp[i - 1, 0]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i, 1] += dp[i - 1, 1]; 
            dp[i, 0] += dp[i - 1, 0]; 
        
        else
        
  
            // Considering i-th element 
            // will be present in 
            // the subsequence 
            dp[i, 0] += 1; 
  
            // Appending i-th element to all 
            // non-empty subsequences 
            // ending with odd element 
            // till (i-1)th indexes 
            dp[i, 0] += dp[i - 1, 1]; 
  
            // Considering ith element will 
            // not be present in 
            // the subsequence 
            dp[i, 0] += dp[i - 1, 0]; 
            dp[i, 1] += dp[i - 1, 1]; 
        
    
  
    // Count of all valid subsequences 
    return (int)(dp[n, 0] + dp[n, 1]); 
}
  
// Driver code 
public static void Main() 
{
    int[] arr = { 5, 6, 9, 7 };
    int n = arr.Length;
          
    Console.Write(validsubsequences(arr, n));
}
}
  
// This code is contributed by chitranayal

chevron_right


Output: 

9

Time complexity: O(N) 
Auxiliary Space complexity: O(N)
 

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.