Open In App

Count of all subsequences having adjacent elements with different parity

Last Updated : 11 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 elements of the subsequence have the 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: 9  


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++
// 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; 
} 
Java
// 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
Python3
# 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
C#
// 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
JavaScript
<script>

// Javascript program for the above approach
 
// Function to find required subsequences     
function 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 
    let dp = new Array(n + 1); 
    for (var i = 0; i < dp.length; i++) {
    dp[i] = new Array(2);
    }
  
    // Initialise the dp[][] with 0. 
    for(let i = 0; i < n + 1; i++)
    { 
        dp[i][0] = 0; 
        dp[i][1] = 0; 
    }
      
    for(let 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 (dp[n][0] + dp[n][1]); 
}

// Driver Code

    let arr = [ 5, 6, 9, 7 ];
    let n = arr.length;
          
    document.write(validsubsequences(arr, n));

</script>

Output: 
9

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


Next Article

Similar Reads