Skip to content
Related Articles

Related Articles

Improve Article

Count number of ways to get Odd Sum

  • Difficulty Level : Medium
  • Last Updated : 05 May, 2021

Given N pairs of numbers. The task is to count ways to choose exactly one number from each pair such that the sum of those numbers is odd.
Examples: 
 

Input: 
N = 2 
3 4 
1 2 
Output:
Explanation: 
We can choose 3 from the first pair and 2 from the second pair, and their sum is 5 which is odd. 
Also, we can choose 4 from the first pair and 1 from the second pair, and their sum is 5 which is odd. 
So the total possible ways will be 2.
Input: 
N = 2 
2 2 
2 2 
Output:
 

 

Approach: 
 

  • We will use dynamic programming here, where dp[i][0] will store number of possible ways to get even sum upto i’th pair and dp[i][1] will store number of possible ways to get odd sum upto i’th pair.
  • cnt[i][0] will store count of even numbers in i’th pair and cnt[i][1] will store count of odd numbers in i’th pair.
  • It is known that the sum of two even or sum of two odd will always be even and the sum of one even and one odd will always be odd.
  • We apply this to store the count in the DP array.
  • Ways to get even sum upto i’th pair will be dp[i – 1][0] * cnt[i][0] + dp[i – 1][1] * cnt[i][1].
  • Ways to get odd sum upto i’th pair will be dp[i – 1][1] * cnt[i][0] + dp[i – 1][0] * cnt[i][1].

Below is the implementation of above Approach: 
 



C++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
 
// Count the ways to sum up with odd
// by choosing one element form each
// pair
int CountOfOddSum(int a[][2], int n)
{
    int dp[n][2], cnt[n][2];
 
    // Initialize two array with 0
    memset(dp, 0, sizeof(dp));
    memset(cnt, 0, sizeof(cnt));
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2; j++) {
 
            // if element is even
            if (a[i][j] % 2 == 0) {
 
                // store count of even
                // number in i'th pair
                cnt[i][0]++;
            }
 
            // if the element is odd
            else {
 
                // store count of odd
                // number in i'th pair
                cnt[i][1]++;
            }
        }
    }
 
    // Initial state of dp array
    dp[0][0] = cnt[0][0], dp[0][1] = cnt[0][1];
 
    for (int i = 1; i < n; i++) {
 
        // dp[i][0] = total number of ways
        // to get even sum upto i'th pair
        dp[i][0] = (dp[i - 1][0] * cnt[i][0]
                    + dp[i - 1][1] * cnt[i][1]);
 
        // dp[i][1] = total number of ways
        // to odd even sum upto i'th pair
        dp[i][1] = (dp[i - 1][0] * cnt[i][1]
                    + dp[i - 1][1] * cnt[i][0]);
    }
 
    // dp[n - 1][1] = total number of ways
    // to get odd sum upto n'th pair
    return dp[n - 1][1];
}
 
// Driver code
int main()
{
 
    int a[][2] = { { 1, 2 }, { 3, 6 } };
    int n = sizeof(a) / sizeof(a[0]);
 
    int ans = CountOfOddSum(a, n);
 
    cout << ans << "\n";
 
    return 0;
}

Java




// Java implementation of above approach
class GFG
{
    // Count the ways to sum up with odd
    // by choosing one element form each
    // pair
    static int CountOfOddSum(int a[][], int n)
    {
        int [][]dp = new int[n][2];
        int [][]cnt = new int[n][2];
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 2; j++)
            {
     
                // if element is even
                if (a[i][j] % 2 == 0)
                {
     
                    // store count of even
                    // number in i'th pair
                    cnt[i][0]++;
                }
     
                // if the element is odd
                else
                {
     
                    // store count of odd
                    // number in i'th pair
                    cnt[i][1]++;
                }
            }
        }
     
        // Initial state of dp array
        dp[0][0] = cnt[0][0];
        dp[0][1] = cnt[0][1];
     
        for (int i = 1; i < n; i++)
        {
     
            // dp[i][0] = total number of ways
            // to get even sum upto i'th pair
            dp[i][0] = (dp[i - 1][0] * cnt[i][0] +
                        dp[i - 1][1] * cnt[i][1]);
     
            // dp[i][1] = total number of ways
            // to odd even sum upto i'th pair
            dp[i][1] = (dp[i - 1][0] * cnt[i][1] +
                        dp[i - 1][1] * cnt[i][0]);
        }
     
        // dp[n - 1][1] = total number of ways
        // to get odd sum upto n'th pair
        return dp[n - 1][1];
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a[][] = {{ 1, 2 }, { 3, 6 }};
        int n = a.length;
     
        int ans = CountOfOddSum(a, n);
     
        System.out.println(ans);
    }
}
 
// This code is contributed by ihritik

Python3




# Python3 implementation of the above approach
 
# Count the ways to sum up with odd
# by choosing one element form each
# pair
def CountOfOddSum(a, n):
 
    dp = [[0 for i in range(2)]
             for i in range(n)]
    cnt = [[0 for i in range(2)]
              for i in range(n)]
 
    # Initialize two array with 0
    for i in range(n):
        for j in range(2):
             
            # if element is even
            if (a[i][j] % 2 == 0):
 
                #store count of even
                #number in i'th pair
                cnt[i][0] += 1
 
            # if the element is odd
            else :
 
                # store count of odd
                # number in i'th pair
                cnt[i][1] += 1
 
    # Initial state of dp array
    dp[0][0] = cnt[0][0]
    dp[0][1] = cnt[0][1]
 
    for i in range(1, n):
 
        # dp[i][0] = total number of ways
        # to get even sum upto i'th pair
        dp[i][0] = (dp[i - 1][0] * cnt[i][0] +
                    dp[i - 1][1] * cnt[i][1])
 
        # dp[i][1] = total number of ways
        # to odd even sum upto i'th pair
        dp[i][1] = (dp[i - 1][0] * cnt[i][1] +
                    dp[i - 1][1] * cnt[i][0])
 
    # dp[n - 1][1] = total number of ways
    # to get odd sum upto n'th pair
    return dp[n - 1][1]
 
# Driver code
a = [[1, 2] , [3, 6] ]
n = len(a)
 
ans = CountOfOddSum(a, n)
 
print(ans)
     
# This code is contributed by Mohit Kumar

C#




// C# implementation of above approach
using System;
 
class GFG
{
    // Count the ways to sum up with odd
    // by choosing one element form each
    // pair
    static int CountOfOddSum(int [ , ] a, int n)
    {
        int [ , ]dp = new int[n, 2];
        int [ , ]cnt = new int[n, 2];
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 2; j++)
            {
     
                // if element is even
                if (a[i, j] % 2 == 0)
                {
     
                    // store count of even
                    // number in i'th pair
                    cnt[i, 0]++;
                }
     
                // if the element is odd
                else
                {
     
                    // store count of odd
                    // number in i'th pair
                    cnt[i, 1]++;
                }
            }
        }
     
        // Initial state of dp array
        dp[0, 0] = cnt[0, 0];
        dp[0, 1] = cnt[0, 1];
     
        for (int i = 1; i < n; i++)
        {
     
            // dp[i, 0] = total number of ways
            // to get even sum upto i'th pair
            dp[i, 0] = (dp[i - 1, 0] * cnt[i, 0] +
                        dp[i - 1, 1] * cnt[i, 1]);
     
            // dp[i, 1] = total number of ways
            // to odd even sum upto i'th pair
            dp[i, 1] = (dp[i - 1, 0] * cnt[i, 1] +
                        dp[i - 1, 1] * cnt[i, 0]);
        }
     
        // dp[n - 1, 1] = total number of ways
        // to get odd sum upto n'th pair
        return dp[n - 1, 1];
    }
     
    // Driver code
    public static void Main ()
    {
        int [ , ] a = { { 1, 2 }, { 3, 6 } };
        int n = a.GetLength(1);
     
        int ans = CountOfOddSum(a, n);
     
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by ihritik

Javascript




<script>
 
// Javascript implementation
 
// Count the ways to sum up with odd
// by choosing one element form each
// pair
function CountOfOddSum(a, n)
{
    let dp = new Array(n), cnt = new Array(n);
    for (let i = 0; i < n; i++) {
        dp[i] = new Array(2).fill(0);
        cnt[i] = new Array(2).fill(0);
    }
 
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < 2; j++) {
 
            // if element is even
            if (a[i][j] % 2 == 0) {
 
                // store count of even
                // number in i'th pair
                cnt[i][0]++;
            }
 
            // if the element is odd
            else {
 
                // store count of odd
                // number in i'th pair
                cnt[i][1]++;
            }
        }
    }
 
    // Initial state of dp array
    dp[0][0] = cnt[0][0], dp[0][1] = cnt[0][1];
 
    for (let i = 1; i < n; i++) {
 
        // dp[i][0] = total number of ways
        // to get even sum upto i'th pair
        dp[i][0] = (dp[i - 1][0] * cnt[i][0]
                    + dp[i - 1][1] * cnt[i][1]);
 
        // dp[i][1] = total number of ways
        // to odd even sum upto i'th pair
        dp[i][1] = (dp[i - 1][0] * cnt[i][1]
                    + dp[i - 1][1] * cnt[i][0]);
    }
 
    // dp[n - 1][1] = total number of ways
    // to get odd sum upto n'th pair
    return dp[n - 1][1];
}
 
// Driver code
 
    let a = [ [ 1, 2 ], [ 3, 6 ] ];
    let n = a.length;
 
    let ans = CountOfOddSum(a, n);
 
    document.write(ans);
     
</script>
Output: 
2

 

Time Complexity: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :