Number of Subsequences with Even and Odd Sum
Your task is to determine the number of subsequences with odd sums and the number of subsequences with even sums from a given array of positive integers. As resulting numbers can be large, return both counts modulo 109 + 7.
Definition:
- An even sum subsequence is one where the total sum of elements is divisible by 2.
- An odd sum subsequence is one where the total sum leaves a remainder of 1 when divided by 2.
Example:
Input:
T = 1
N = 3
array = [1, 2, 3]
Output:
3 4
Explanation:
The possible non-empty subsequences are {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}. Out of these, the odd sum subsequences are {1}, {3}, {2,3}, {1,2,3} and the even sum ones are {2}, {1,2}, {1,3}.
Constraints:
1 <= T <= 100
1 <= N <= 104
1 <= ARR[i] <= 109
Note:
- There are
2N - 1
non-empty subsequences for an array of sizeN
. - Subsequences can contain duplicate elements from the array.
- The modulo operation 109 + 7 ensures the numbers remain manageable.
- The function that you implement does not need to handle I/O operations.
Be the first one to answer
Add answer anonymously...
Top Dunzo SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app