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: 9
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++
#include <bits/stdc++.h>
using namespace std;
int validsubsequences( int arr[], int n)
{
long long int dp[n + 1][2];
for ( int i = 0; i < n + 1; i++) {
dp[i][0] = 0;
dp[i][1] = 0;
}
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] % 2) {
dp[i][1] += 1;
dp[i][1] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
}
else {
dp[i][0] += 1;
dp[i][0] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
}
}
return dp[n][0] + dp[n][1];
}
int main()
{
int arr[] = { 5, 6, 9, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << validsubsequences(arr, n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int validsubsequences( int arr[], int n)
{
long dp[][] = new long [n + 1 ][ 2 ];
for ( int i = 0 ; i < n + 1 ; i++)
{
dp[i][ 0 ] = 0 ;
dp[i][ 1 ] = 0 ;
}
for ( int i = 1 ; i <= n; i++)
{
if (arr[i - 1 ] % 2 != 0 )
{
dp[i][ 1 ] += 1 ;
dp[i][ 1 ] += dp[i - 1 ][ 0 ];
dp[i][ 1 ] += dp[i - 1 ][ 1 ];
dp[i][ 0 ] += dp[i - 1 ][ 0 ];
}
else
{
dp[i][ 0 ] += 1 ;
dp[i][ 0 ] += dp[i - 1 ][ 1 ];
dp[i][ 0 ] += dp[i - 1 ][ 0 ];
dp[i][ 1 ] += dp[i - 1 ][ 1 ];
}
}
return ( int )(dp[n][ 0 ] + dp[n][ 1 ]);
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 9 , 7 };
int n = arr.length;
System.out.print(validsubsequences(arr, n));
}
}
|
Python3
def validsubsequences(arr, n):
dp = [[ 0 for i in range ( 2 )]
for j in range (n + 1 )]
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] % 2 ):
dp[i][ 1 ] + = 1
dp[i][ 1 ] + = dp[i - 1 ][ 0 ]
dp[i][ 1 ] + = dp[i - 1 ][ 1 ]
dp[i][ 0 ] + = dp[i - 1 ][ 0 ]
else :
dp[i][ 0 ] + = 1
dp[i][ 0 ] + = dp[i - 1 ][ 1 ]
dp[i][ 0 ] + = dp[i - 1 ][ 0 ]
dp[i][ 1 ] + = dp[i - 1 ][ 1 ]
return dp[n][ 0 ] + dp[n][ 1 ]
if __name__ = = '__main__' :
arr = [ 5 , 6 , 9 , 7 ]
n = len (arr)
print (validsubsequences(arr, n))
|
C#
using System;
class GFG{
static int validsubsequences( int [] arr, int n)
{
long [,] dp = new long [n + 1, 2];
for ( int i = 0; i < n + 1; i++)
{
dp[i, 0] = 0;
dp[i, 1] = 0;
}
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 != 0)
{
dp[i, 1] += 1;
dp[i, 1] += dp[i - 1, 0];
dp[i, 1] += dp[i - 1, 1];
dp[i, 0] += dp[i - 1, 0];
}
else
{
dp[i, 0] += 1;
dp[i, 0] += dp[i - 1, 1];
dp[i, 0] += dp[i - 1, 0];
dp[i, 1] += dp[i - 1, 1];
}
}
return ( int )(dp[n, 0] + dp[n, 1]);
}
public static void Main()
{
int [] arr = { 5, 6, 9, 7 };
int n = arr.Length;
Console.Write(validsubsequences(arr, n));
}
}
|
Javascript
<script>
function validsubsequences(arr, n)
{
let dp = new Array(n + 1);
for ( var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (let i = 0; i < n + 1; i++)
{
dp[i][0] = 0;
dp[i][1] = 0;
}
for (let i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 != 0)
{
dp[i][1] += 1;
dp[i][1] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
}
else
{
dp[i][0] += 1;
dp[i][0] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
}
}
return (dp[n][0] + dp[n][1]);
}
let arr = [ 5, 6, 9, 7 ];
let n = arr.length;
document.write(validsubsequences(arr, n));
</script>
|
Time complexity: O(N)
Auxiliary Space complexity: O(N)