Count of permutations such that sum of K numbers from given range is even
Given a range [low, high], both inclusive, and an integer K, the task is to select K numbers from the range(a number can be chosen multiple times) such that the sum of those K numbers is even. Print the number of all such permutations.
Examples:
Input: low = 4, high = 5, k = 3
Output: 4
Explanation:
There are 4 valid permutation. They are {4, 4, 4}, {4, 5, 5}, {5, 4, 5} and {5, 5, 4} which sum up to an even number.
Input: low = 1, high = 10, k = 2
Output: 50
Explanation:
There are 50 valid permutations. They are {1, 1}, {1, 3}, .. {1, 9} {2, 2}, {2, 4}, …, {2, 10}, …, {10, 2}, {10, 4}, … {10, 10}.
These 50 permutations, each sum up to an even number.
Naive Approach: The idea is to find all subset of size K such that the sum of the subset is even and also calculate permutation for each required subset.
Time Complexity: O(K * (2K))
Auxiliary Space: O(K)
Efficient Approach: The idea is to use the fact that the sum of two even and odd numbers is always even. Follow the steps below to solve the problem:
- Find the total count of even and odd numbers in the given range [low, high].
- Initialize variable even_sum = 1 and odd_sum = 0 to store way to get even sum and odd sum respectively.
- Iterate a loop K times and store the previous even sum as prev_even = even_sum and the previous odd sum as prev_odd = odd_sum where even_sum = (prev_even*even_count) + (prev_odd*odd_count) and odd_sum = (prev_even*odd_count) + (prev_odd*even_count).
- Print the even_sum at the end as there is a count for the odd sum because the previous odd_sum will contribute to the next even_sum.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countEvenSum( int low, int high, int k)
{
int even_count = high / 2 - (low - 1) / 2;
int odd_count = (high + 1) / 2 - low / 2;
long even_sum = 1;
long odd_sum = 0;
for ( int i = 0; i < k; i++)
{
long prev_even = even_sum;
long prev_odd = odd_sum;
even_sum = (prev_even * even_count) +
(prev_odd * odd_count);
odd_sum = (prev_even * odd_count) +
(prev_odd * even_count);
}
cout << (even_sum);
}
int main()
{
int low = 4;
int high = 5;
int K = 3;
countEvenSum(low, high, K);
}
|
Java
import java.util.*;
class GFG {
public static void
countEvenSum( int low, int high,
int k)
{
int even_count = high / 2 - (low - 1 ) / 2 ;
int odd_count = (high + 1 ) / 2 - low / 2 ;
long even_sum = 1 ;
long odd_sum = 0 ;
for ( int i = 0 ; i < k; i++) {
long prev_even = even_sum;
long prev_odd = odd_sum;
even_sum = (prev_even * even_count)
+ (prev_odd * odd_count);
odd_sum = (prev_even * odd_count)
+ (prev_odd * even_count);
}
System.out.println(even_sum);
}
public static void main(String[] args)
{
int low = 4 ;
int high = 5 ;
int K = 3 ;
countEvenSum(low, high, K);
}
}
|
Python3
def countEvenSum(low, high, k):
even_count = high / 2 - (low - 1 ) / 2
odd_count = (high + 1 ) / 2 - low / 2
even_sum = 1
odd_sum = 0
for i in range ( 0 , k):
prev_even = even_sum
prev_odd = odd_sum
even_sum = ((prev_even * even_count) +
(prev_odd * odd_count))
odd_sum = ((prev_even * odd_count) +
(prev_odd * even_count))
print ( int (even_sum))
low = 4 ;
high = 5 ;
K = 3 ;
countEvenSum(low, high, K);
|
C#
using System;
class GFG{
public static void countEvenSum( int low,
int high, int k)
{
int even_count = high / 2 - (low - 1) / 2;
int odd_count = (high + 1) / 2 - low / 2;
long even_sum = 1;
long odd_sum = 0;
for ( int i = 0; i < k; i++)
{
long prev_even = even_sum;
long prev_odd = odd_sum;
even_sum = (prev_even * even_count) +
(prev_odd * odd_count);
odd_sum = (prev_even * odd_count) +
(prev_odd * even_count);
}
Console.WriteLine(even_sum);
}
public static void Main(String[] args)
{
int low = 4;
int high = 5;
int K = 3;
countEvenSum(low, high, K);
}
}
|
Javascript
<script>
function
countEvenSum(low, high, k)
{
let even_count = high / 2 - (low - 1) / 2;
let odd_count = (high + 1) / 2 - low / 2;
let even_sum = 1;
let odd_sum = 0;
for (let i = 0; i < k; i++) {
let prev_even = even_sum;
let prev_odd = odd_sum;
even_sum = (prev_even * even_count)
+ (prev_odd * odd_count);
odd_sum = (prev_even * odd_count)
+ (prev_odd * even_count);
}
document.write(even_sum);
}
let low = 4;
let high = 5;
let K = 3;
countEvenSum(low, high, K);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Last Updated :
29 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...