📌  相关文章
📜  检查是否可以将给定数组拆分为K个奇数和子集

📅  最后修改于: 2021-04-27 23:26:31             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是检查是否有可能将给定数组拆分为K个非空和不相交的子集,以使每个子集的元素之和为奇数。

例子:

方法:
为了解决上述问题,我们需要注意以下几点:

  • 偶数不会改变子集总和的奇偶校验,因此我们可以忽略它们。
  • 如果数组中奇数个数小于K ,则由于奇数个数不足,我们无法将其分成具有奇数和的K个子集。
  • 令奇数个整数为cnt 。这样,只有当cnt%2 = K%2时,答案才总是可能的。这是因为我们将在前K-1个子集中分配一个奇数,而在最后一个子集中分配cnt – K – 1个奇数。现在,由于cntK具有相同的奇偶校验,因此cnt – K – 1将为奇数,并且总和也为奇数。

因此,要解决该问题,请计算数组中存在的奇数整数的数量。让它成为cnt 。如果cnt大于Kcnt%2 = K%2 ,则答案为“是”。否则,将无法回答,我们将打印“否”。

下面是上述方法的实现:

C++
// C++ implementation to check if it is
// possible to split array into K
// subsets with odd sum
#include 
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


输出:
Yes

时间复杂度: O(N)