📌  相关文章
📜  检查数组中正好K个元素的总和是否为奇数

📅  最后修改于: 2021-04-26 08:09:49             🧑  作者: Mango

给定一个数组arr []和一个整数K。通过精确选择数组的K个元素,检查是否有可能获得奇数和。

例子:

方法:观察发现存在三种情况。

  • 首先,计算奇数和偶数元素的数量。
  • 情况1:当所有元素都是偶数时。然后,总和将始终为偶数,而与K的值无关,因为偶数+偶数=偶数
  • 情况2:所有元素均为奇数时。然后,总和将仅取决于K的值。
    如果K为奇数,则总和将为奇数,因为每个奇数对使和为偶数,最后,一个奇数元素使和为奇数,即偶数+奇数=奇数
    如果K为偶数,则每个奇数元素都成对并变为偶数,因此总和变为偶数。
  • 情况3:当K <= N时,总和仅取决于奇数元素的数量。如果奇数元素的数量为偶数,则总和将为奇数+奇数=偶数,这意味着每个奇数对将变为偶数。如果我们将和添加偶数元素,则总和将保持为偶数+偶数=偶数

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function returns true if
// it is possible to have
// odd sum
bool isPossible(int arr[],
                int N, int K)
{
    int oddCount = 0, evenCount = 0;
 
    // counting number of odd
    // and even elements
    for (int i = 0; i < N; i++) {
        if (arr[i] % 2 == 0)
            evenCount++;
        else
            oddCount++;
    }
    if (evenCount == N
        || (oddCount == N && K % 2 == 0)
        || (K == N && oddCount % 2 == 0))
        return false;
    else
        return true;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 8 };
    int K = 5;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    if (isPossible(arr, N, K))
        cout << "Possible";
    else
        cout << "Not Possible";
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function returns true if
// it is possible to have
// odd sum
static boolean isPossible(int arr[],
                          int N, int K)
{
    int oddCount = 0, evenCount = 0;
     
    // Counting number of odd
    // and even elements
    for(int i = 0; i < N; i++)
    {
       if (arr[i] % 2 == 0)
       {
           evenCount++;
       }
       else
       {
           oddCount++;
       }
    }
    if (evenCount == N ||
       (oddCount == N && K % 2 == 0) ||
       (K == N && oddCount % 2 == 0))
    {
        return false;
    }
    else
    {
        return true;
    }
}
     
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, 8 };
    int K = 5;
    int N = arr.length;
     
    if (isPossible(arr, N, K))
    {
        System.out.println("Possible");
    }
    else
    {
        System.out.println("Not Possible");
    }
}
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the above approach
 
# Function returns true if it
# is possible to have odd sum
def isPossible(arr, N, K):
     
    oddCount = 0
    evenCount = 0
 
    # Counting number of odd
    # and even elements
    for i in range(N):
        if (arr[i] % 2 == 0):
            evenCount += 1
        else:
            oddCount += 1
             
    if (evenCount == N or
       (oddCount == N and K % 2 == 0) or
       (K == N and oddCount % 2 == 0)):
        return False
    else:
        return True
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 5, 8 ]
    K = 5
    N = len(arr)
 
    if (isPossible(arr, N, K)):
        print("Possible")
    else:
        print("Not Possible")
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
// Function returns true if
// it is possible to have
// odd sum
static bool isPossible(int []arr,
                       int N, int K)
{
    int oddCount = 0, evenCount = 0;
 
    // Counting number of odd
    // and even elements
    for(int i = 0; i < N; i++)
    {
       if (arr[i] % 2 == 0)
       {
           evenCount++;
       }
       else
       {
           oddCount++;
       }
    }
    if (evenCount == N ||
       (oddCount == N && K % 2 == 0) ||
       (K == N && oddCount % 2 == 0))
    {
        return false;
    }
    else
    {
        return true;
    }
}
 
// Driver code
public static void Main (string[] args)
{
    int []arr = { 1, 2, 3, 4, 5, 8 };
    int K = 5;
    int N = arr.Length;
 
    if (isPossible(arr, N, K))
    {
        Console.WriteLine("Possible");
    }
    else
    {
        Console.WriteLine("Not Possible");
    }
}
}
 
// This code is contributed by AnkitRai01


输出
Possible

时间复杂度: O(N)