Check if it is possible to split given Array into K odd-sum subsets
Last Updated :
21 Apr, 2021
Given an array arr[] of length N, the task is to check if it is possible to split the given array into K non-empty and non-intersecting subsets such that the sum of elements of each subset is odd.
Examples:
Input: K = 4, arr[] = {1, 3, 4, 7, 5, 3, 1}
Output: Yes
Explanation:
[1], [3, 4, 7, 5], [3] and [1] are the possible subsets.
Input: K = 3, arr[] = {2, 3, 4, 7, 2}
Output: No
Explanation:
Given array cannot be split into 3 subset with odd sum.
Approach:
To solve the problem mentioned above we need to observe the following points:
- Even numbers don’t change the parity of the sum of subsets, so we can ignore them.
- If number of odd integers in the array is less than K, then we cannot split it into K subsets with odd sums since there are not enough odd integers.
- Let number of odd integers be cnt. Then, the answer will always be possible only if cnt % 2 = K % 2. This is because we will distribute one odd number in the first K-1 subsets and cnt – K – 1 odd numbers in the last subset. Now since cnt and K have the same parity so cnt – K – 1 will be odd and the sum is also odd.
Hence, to solve the problem, count the number of odd integers present in the array. Let this be cnt. The answer will be ‘Yes’ if cnt is greater than K and cnt % 2 = K % 2. Otherwise, an answer is not possible and we print ‘No’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkArray( int n, int k, int arr[])
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] & 1)
cnt += 1;
}
if (cnt >= k && cnt % 2 == k % 2)
return true ;
else
return false ;
}
int main()
{
int arr[] = { 1, 3, 4, 7, 5, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 4;
if (checkArray(n, k, arr))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean checkArray( int n, int k,
int arr[])
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((arr[i] & 1 ) != 0 )
cnt += 1 ;
}
if (cnt >= k && cnt % 2 == k % 2 )
return true ;
else
return false ;
}
public static void main (String []args)
{
int arr[] = { 1 , 3 , 4 , 7 , 5 , 3 , 1 };
int n = arr.length;
int k = 4 ;
if (checkArray(n, k, arr))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkArray(n, k, arr):
cnt = 0
for i in range (n):
if (arr[i] & 1 ):
cnt + = 1
if (cnt > = k and cnt % 2 = = k % 2 ):
return True
else :
return False
if __name__ = = '__main__' :
arr = [ 1 , 3 , 4 , 7 , 5 , 3 , 1 ]
n = len (arr)
k = 4
if (checkArray(n, k, arr)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkArray( int n, int k,
int []arr)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] & 1) != 0)
cnt += 1;
}
if (cnt >= k && cnt % 2 == k % 2)
return true ;
else
return false ;
}
public static void Main ( string []args)
{
int []arr = { 1, 3, 4, 7, 5, 3, 1 };
int n = arr.Length;
int k = 4;
if (checkArray(n, k, arr))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkArray(n , k , arr) {
var cnt = 0;
for (i = 0; i < n; i++) {
if ((arr[i] & 1) != 0)
cnt += 1;
}
if (cnt >= k && cnt % 2 == k % 2)
return true ;
else
return false ;
}
var arr = [ 1, 3, 4, 7, 5, 3, 1 ];
var n = arr.length;
var k = 4;
if (checkArray(n, k, arr))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...