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.
N = 2
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.
N = 2
- We will use dynamic programming here, where dp[i] will store number of possible ways to get even sum upto i’th pair and dp[i] will store number of possible ways to get odd sum upto i’th pair.
- cnt[i] will store count of even numbers in i’th pair and cnt[i] 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] * cnt[i] + dp[i – 1] * cnt[i].
- Ways to get odd sum upto i’th pair will be dp[i – 1] * cnt[i] + dp[i – 1] * cnt[i].
So we can store the dp array in this way,
dp[i] = dp[i - 1] * cnt[i] + dp[i - 1] * cnt[i] dp[i] = dp[i - 1] * cnt[i] + dp[i - 1] * cnt[i]
Below is the implementation of above Approach:
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.
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number of ways to get a given sum with n number of m-faced dices
- Count ways to make sum of odd and even indexed elements equal by removing an array element
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
- Count numbers from given range having odd digits at odd places and even digits at even places
- Average of odd numbers till a given odd number
- Split the array into odd number of segments of odd lengths
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Count ways to express a number as sum of consecutive numbers
- Count ways to express even number ‘n’ as sum of even integers
- Count ways to express a number as sum of exactly two numbers
- Count of ways to represent N as sum of a prime number and twice of a square
- Count number of ways to divide an array into two halves with same sum
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Count number of ways to divide a number in 4 parts
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Given two arrays count all pairs whose sum is an odd number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.