Split array into K disjoint subarrays such that sum of each subarray is odd.
Last Updated :
06 Jan, 2023
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 subarrays S1, S2, S3, ..., SK:
- The intersection of S1, S2, S3, ..., SK should be NULL.
- The union of S1, S2, S3, ..., SK should be equal to the array.
Examples:
Input: N = 5, arr[] = {7, 2, 11, 4, 19}, K = 3
Output: 1 3 5
Explanation:
When the given array arr[] is divided into K = 3 parts, the possible subarrays are: {7, 2}, {11, 4} and {19}
Input: N = 5, arr[] = {2, 4, 6, 8, 10}, K = 3
Output: -1
Explanation:
It is impossible to divide the array arr[] into K = 3 subarrays as all the elements are even and the sum of every subarray is even.
Approach: It can be easily observed that for any subarray to have odd sum:
- Since only odd values can lead to odd sum, hence we can ignore the even values.
- The number of odd values must also be odd.
- So we need at least K odd values in the array for K subarrays. If K is greater than the number of odd elements then the answer is always -1.
Below is the implementation of the above approach:
C++
// C++ program to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
#include <iostream>
using namespace std;
// Function to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
void split(int a[], int n, int k)
{
// Number of odd elements
int odd_ele = 0;
// Loop to store the number
// of odd elements in the array
for (int i = 0; i < n; i++)
if (a[i] % 2)
odd_ele++;
// If the count of odd elements is < K
// then the answer doesnt exist
if (odd_ele < k)
cout << -1;
// If the number of odd elements is
// greater than K and the extra
// odd elements are odd, then the
// answer doesn't exist
else if (odd_ele > k && (odd_ele - k) % 2)
cout << -1;
else {
for (int i = 0; i < n; i++) {
if (a[i] % 2) {
// Printing the position of
// odd elements
cout << i + 1 << " ";
// Decrementing K as we need positions
// of only first k odd numbers
k--;
}
// When the positions of the first K
// odd numbers are printed
if (k == 0)
break;
}
}
}
// Driver code
int main()
{
int n = 5;
int arr[] = { 7, 2, 11, 4, 19 };
int k = 3;
split(arr, n, k);
}
Java
// Java program to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
class GFG{
// Function to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
static void split(int a[], int n, int k)
{
// Number of odd elements
int odd_ele = 0;
// Loop to store the number
// of odd elements in the array
for (int i = 0; i < n; i++)
if (a[i] % 2==1)
odd_ele++;
// If the count of odd elements is < K
// then the answer doesnt exist
if (odd_ele < k)
System.out.print(-1);
// If the number of odd elements is
// greater than K and the extra
// odd elements are odd, then the
// answer doesn't exist
else if (odd_ele > k && (odd_ele - k) % 2==1)
System.out.print(-1);
else {
for (int i = 0; i < n; i++) {
if (a[i] % 2==1) {
// Printing the position of
// odd elements
System.out.print(i + 1+ " ");
// Decrementing K as we need positions
// of only first k odd numbers
k--;
}
// When the positions of the first K
// odd numbers are printed
if (k == 0)
break;
}
}
}
// Driver code
public static void main(String[] args)
{
int n = 5;
int arr[] = { 7, 2, 11, 4, 19 };
int k = 3;
split(arr, n, k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to split the array into K
# disjoint subarrays so that the sum of
# each subarray is odd.
# Function to split the array into K
# disjoint subarrays so that the sum of
# each subarray is odd.
def split(a, n, k) :
# Number of odd elements
odd_ele = 0;
# Loop to store the number
# of odd elements in the array
for i in range(n) :
if (a[i] % 2) :
odd_ele += 1;
# If the count of odd elements is < K
# then the answer doesnt exist
if (odd_ele < k) :
print(-1);
# If the number of odd elements is
# greater than K and the extra
# odd elements are odd, then the
# answer doesn't exist
elif (odd_ele > k and (odd_ele - k) % 2) :
print(-1);
else :
for i in range(n) :
if (a[i] % 2) :
# Printing the position of
# odd elements
print(i + 1 ,end= " ");
# Decrementing K as we need positions
# of only first k odd numbers
k -= 1;
# When the positions of the first K
# odd numbers are printed
if (k == 0) :
break;
# Driver code
if __name__ == "__main__" :
n = 5;
arr = [ 7, 2, 11, 4, 19 ];
k = 3;
split(arr, n, k);
# This code is contributed by AnkitRai01
C#
// C# program to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
using System;
class GFG{
// Function to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
static void split(int []a, int n, int k)
{
// Number of odd elements
int odd_ele = 0;
// Loop to store the number
// of odd elements in the array
for (int i = 0; i < n; i++)
if (a[i] % 2 == 1)
odd_ele++;
// If the count of odd elements is < K
// then the answer doesnt exist
if (odd_ele < k)
Console.Write(-1);
// If the number of odd elements is
// greater than K and the extra
// odd elements are odd, then the
// answer doesn't exist
else if (odd_ele > k && (odd_ele - k) % 2 == 1)
Console.Write(-1);
else {
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 1) {
// Printing the position of
// odd elements
Console.Write(i + 1 + " ");
// Decrementing K as we need positions
// of only first k odd numbers
k--;
}
// When the positions of the first K
// odd numbers are printed
if (k == 0)
break;
}
}
}
// Driver code
public static void Main(string[] args)
{
int n = 5;
int []arr = { 7, 2, 11, 4, 19 };
int k = 3;
split(arr, n, k);
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript program to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
// Function to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
function split(a, n, k)
{
// Number of odd elements
let odd_ele = 0;
// Loop to store the number
// of odd elements in the array
for (let i = 0; i < n; i++)
if (a[i] % 2)
odd_ele++;
// If the count of odd elements is < K
// then the answer doesnt exist
if (odd_ele < k)
document.write(-1);
// If the number of odd elements is
// greater than K and the extra
// odd elements are odd, then the
// answer doesn't exist
else if (odd_ele > k && (odd_ele - k) % 2)
document.write(1);
else {
for (let i = 0; i < n; i++) {
if (a[i] % 2) {
// Printing the position of
// odd elements
document.write(i + 1 + " ");
// Decrementing K as we need positions
// of only first k odd numbers
k--;
}
// When the positions of the first K
// odd numbers are printed
if (k == 0)
break;
}
}
}
// Driver code
let n = 5;
let arr = [ 7, 2, 11, 4, 19 ];
let k = 3;
split(arr, n, k);
// This code is contributed by gfgking
</script>
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Split array into K subarrays such that sum of maximum of all subarrays is maximized Given an array arr[] of size N and a number K, the task is to partition the given array into K contiguous subarrays such that the sum of the maximum of each subarray is the maximum possible. If it is possible to split the array in such a manner, then print the maximum possible sum. Otherwise, print
10 min read
Split the array into N subarrays where each subarray has size 1 or sum at least K Given an array arr[] of size N and integer K. Task for this problem is to check if given array can be split into N continuous subarrays by performing given operations. In one operation choose existing array which may be result of previous operation and split it into two subarrays with each of subarr
10 min read
Count ways to split an array into subarrays such that sum of the i-th subarray is divisible by i Given an array arr[] consisting of N integers, the task is to find the number of ways to split the array into non-empty subarrays such that the sum of the ith subarray is divisible by i. Examples: Input: arr[] = {1, 2, 3, 4}Output: 3Explanation:Following are the number of ways to split the array int
8 min read
Split Array into maximum Subarrays so that sum of alternating sums is 0 Given an array arr[] of 1s and -1s, the task is to partition the array into maximum subarrays such that the sum of the alternating sum of all the subarrays is 0. Print the ranges of the subarrays and the number of subarrays. Note: The alternating sum of a subarray a[] is defined as a[0] - a[1] + a[2
7 min read
Split into K subarrays to minimize the maximum sum of all subarrays Given an array arr[] and a number k, split the given array into k subarrays such that the maximum subarray sum achievable out of k subarrays formed is the minimum possible. The task is to find that possible subarray sum.Examples:Input: arr[] = [1, 2, 3, 4], k = 3 Output: 4 Explanation: Optimal Split
11 min read