📌  相关文章
📜  检查数组的恰好 K 个元素的总和是否可以是奇数

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

给定一个数组arr[]和一个整数K 。检查是否可以通过选择数组中的K 个元素来获得奇数和。

例子:

处理方法:观察,发现有3种情况。

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

下面是上述方法的实现:

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


Javascript


输出
Possible

时间复杂度: O(N)

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