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++
// C++ implementation to check if it is
// possible to split array into K
// subsets with odd sum
#include <bits/stdc++.h>
using namespace std;
// Function to check if array
// can be split in required K
// subsets
bool checkArray(int n, int k, int arr[])
{
// Store count of
// odd numbers
int cnt = 0;
for (int i = 0; i < n; i++) {
// Check if element
// is odd
if (arr[i] & 1)
cnt += 1;
}
// Check if split is possible
if (cnt >= k && cnt % 2 == k % 2)
return true;
else
return false;
}
// Driver Program
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
// Java implementation to check if it
// is possible to split array into K
// subsets with odd sum
class GFG{
// Function to check if array
// can be split in required K
// subsets
static boolean checkArray(int n, int k,
int arr[])
{
// Store count of odd numbers
int cnt = 0;
for(int i = 0; i < n; i++)
{
// Check if element is odd
if ((arr[i] & 1) != 0)
cnt += 1;
}
// Check if split is possible
if (cnt >= k && cnt % 2 == k % 2)
return true;
else
return false;
}
// Driver code
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");
}
}
// This code is contributed by chitranayal
Python3
# Python3 implementation to check if
# it is possible to split array into
# K subsets with odd sum
# Function to check if array
# can be split in required K
# subsets
def checkArray(n, k, arr):
# Store count of
# odd numbers
cnt = 0
for i in range(n):
# Check if element
# is odd
if (arr[i] & 1):
cnt += 1
# Check if split is possible
if (cnt >= k and cnt % 2 == k % 2):
return True
else:
return False
# Driver Code
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")
# This code is contributed by mohit kumar 29
C#
// C# implementation to check if it
// is possible to split array into K
// subsets with odd sum
using System;
class GFG{
// Function to check if array
// can be split in required K
// subsets
static bool checkArray(int n, int k,
int []arr)
{
// Store count of odd numbers
int cnt = 0;
for(int i = 0; i < n; i++)
{
// Check if element is odd
if ((arr[i] & 1) != 0)
cnt += 1;
}
// Check if split is possible
if (cnt >= k && cnt % 2 == k % 2)
return true;
else
return false;
}
// Driver code
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");
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// javascript implementation to check if it
// is possible to split array into K
// subsets with odd sum
// Function to check if array
// can be split in required K
// subsets
function checkArray(n , k , arr) {
// Store count of odd numbers
var cnt = 0;
for (i = 0; i < n; i++) {
// Check if element is odd
if ((arr[i] & 1) != 0)
cnt += 1;
}
// Check if split is possible
if (cnt >= k && cnt % 2 == k % 2)
return true;
else
return false;
}
// Driver code
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");
// This code contributed by gauravrajput1
</script>
Time Complexity: O(N)
Similar Reads
Check if it possible to partition in k subarrays with equal sum Given an array A of size N, and a number K. Task is to find out if it is possible to partition the array A into K contiguous subarrays such that the sum of elements within each of these subarrays is the same. Prerequisite: Count the number of ways to divide an array into three contiguous parts havin
15+ min read
Check if String can be divided into two Subsequences so that product of sum is odd Given a string S of length N, the task is to check whether it is possible to partition the string S into two disjoint non-empty subsequences S1 and S2 such that sum(S1) Ã sum(S2) is odd and every character of S must be in either S2 and S1. Examples: Input: S = "1122"Output: Yes?Explanation: We parti
5 min read
Split array into K-length subsets to minimize sum of second smallest element of each subset Given an array arr[] of size N and an integer K (N % K = 0), the task is to split array into subarrays of size K such that the sum of 2nd smallest elements of each subarray is the minimum possible. Examples: Input: arr[] = {11, 20, 5, 7, 8, 14, 2, 17, 16, 10}, K = 5Output: 13Explanation: Splitting a
5 min read
Print all possible ways to split an array into K subsets Given an array arr[] of size N and an integer K, the task is to print all possible ways to split the given array into K subsets. Examples: Input: arr[] = { 1, 2, 3 }, K = 2Output: { {{ 1, 2 }, { 3 }}, {{ 1, 3 }, { 2 }}, {{ 1 }, { 2, 3 }}}. Input: arr[] = { 1, 2, 3, 4 }, K = 2Output: { {{ 1, 2, 3 },
13 min read
Split array into K disjoint subarrays such that sum of each subarray is odd. Given an array arr[] containing N elements, the task is to divide the array into K(1 ? K ? N) subarrays and such that the sum of elements of each subarray is odd. Print the starting index (1 based indexing) of each subarray after dividing the array and -1 if no such subarray exists.Note: For all sub
8 min read