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 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++
#include <iostream>
using namespace std;
void split( int a[], int n, int k)
{
int odd_ele = 0;
for ( int i = 0; i < n; i++)
if (a[i] % 2)
odd_ele++;
if (odd_ele < k)
cout << -1;
else if (odd_ele > k && (odd_ele - k) % 2)
cout << -1;
else {
for ( int i = 0; i < n; i++) {
if (a[i] % 2) {
cout << i + 1 << " " ;
k--;
}
if (k == 0)
break ;
}
}
}
int main()
{
int n = 5;
int arr[] = { 7, 2, 11, 4, 19 };
int k = 3;
split(arr, n, k);
}
|
Java
class GFG{
static void split( int a[], int n, int k)
{
int odd_ele = 0 ;
for ( int i = 0 ; i < n; i++)
if (a[i] % 2 == 1 )
odd_ele++;
if (odd_ele < k)
System.out.print(- 1 );
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 ) {
System.out.print(i + 1 + " " );
k--;
}
if (k == 0 )
break ;
}
}
}
public static void main(String[] args)
{
int n = 5 ;
int arr[] = { 7 , 2 , 11 , 4 , 19 };
int k = 3 ;
split(arr, n, k);
}
}
|
Python3
def split(a, n, k) :
odd_ele = 0 ;
for i in range (n) :
if (a[i] % 2 ) :
odd_ele + = 1 ;
if (odd_ele < k) :
print ( - 1 );
elif (odd_ele > k and (odd_ele - k) % 2 ) :
print ( - 1 );
else :
for i in range (n) :
if (a[i] % 2 ) :
print (i + 1 ,end = " " );
k - = 1 ;
if (k = = 0 ) :
break ;
if __name__ = = "__main__" :
n = 5 ;
arr = [ 7 , 2 , 11 , 4 , 19 ];
k = 3 ;
split(arr, n, k);
|
C#
using System;
class GFG{
static void split( int []a, int n, int k)
{
int odd_ele = 0;
for ( int i = 0; i < n; i++)
if (a[i] % 2 == 1)
odd_ele++;
if (odd_ele < k)
Console.Write(-1);
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) {
Console.Write(i + 1 + " " );
k--;
}
if (k == 0)
break ;
}
}
}
public static void Main( string [] args)
{
int n = 5;
int []arr = { 7, 2, 11, 4, 19 };
int k = 3;
split(arr, n, k);
}
}
|
Javascript
<script>
function split(a, n, k)
{
let odd_ele = 0;
for (let i = 0; i < n; i++)
if (a[i] % 2)
odd_ele++;
if (odd_ele < k)
document.write(-1);
else if (odd_ele > k && (odd_ele - k) % 2)
document.write(1);
else {
for (let i = 0; i < n; i++) {
if (a[i] % 2) {
document.write(i + 1 + " " );
k--;
}
if (k == 0)
break ;
}
}
}
let n = 5;
let arr = [ 7, 2, 11, 4, 19 ];
let k = 3;
split(arr, n, k);
</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
Last Updated :
06 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...