📜  将数组拆分为 K 个不相交的子数组,使得每个子数组的和为奇数。

📅  最后修改于: 2021-09-05 11:41:22             🧑  作者: Mango

给定一个包含N 个元素的数组arr[] ,任务是将数组划分为K(1 ≤ K ≤ N)个子数组,并且每个子数组的元素之和为奇数。分割数组后打印每个子数组的起始索引(基于 1 的索引),如果不存在这样的子数组,则打印-1
注意:对于所有子数组 S 1 , S 2 , S 3 , …, S K

  • S 1 , S 2 , S 3 , …, S K的交集应为NULL。
  • S 1 , S 2 , S 3 , …, S K 的并集应该等于数组。

例子:

方法:可以很容易地观察到,对于任何具有奇数和的子数组:

  1. 由于只有奇数才能导致奇数和,因此我们可以忽略偶数。
  2. 奇数的数量也必须是奇数。
  3. 因此,对于 K 个子数组,我们需要在数组中至少有 K 个奇数值。如果 K 大于奇数元素的数量,则答案始终为-1

下面是上述方法的实现:

C++
// C++ program to split the array into K
// disjoint subarrays so that the sum of
// each subarray is odd.
 
#include 
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


输出:
1 3 5

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live