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++
// 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 codeint 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 codepublic 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 codeif __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 codepublic 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> |
1 3 5
Â
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
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



