📌  相关文章
📜  检查是否可以从给定数组中选择带有偶数和的N个数字

📅  最后修改于: 2021-05-04 23:12:32             🧑  作者: Mango

给定一个数组arr []和一个奇数N ,任务是检查是否可以从具有偶数sum的数组中选择N个数字。如果可能,请打印。否则打印No。

例子:

方法:请按照以下步骤解决问题:

  1. 计算偶数和奇数整数,并将其分别存储在even_freqodd_freq中
  2. 如果even_freq超过N ,则取所有偶数,它们的和为偶数。因此,打印“是”。
  3. 否则,检查odd_freq
  4. 如果odd_freq奇数,则检查是否(odd_freq + even_freq – 1)≥N或不。如果发现是真的,则打印“”。
  5. 如果odd_freq偶数,则然后检查(odd_freq + even_freq)≥N或不。如果发现是真的,则打印“”。
  6. 如果以上条件都不满足,请打印“否”。

下面是上述方法的实现:

C++
// C++ efficient program to check
// if N numbers with Odd sum can be
// selected from the given array
#include 
using namespace std;
  
// Function to check if an odd sum can be
// made using N integers from the array
bool checkEvenSum(int arr[], int N, int size)
{
    // Initialize odd and even counts
    int even_freq = 0, odd_freq = 0;
  
    // Iterate over the array to count
    // the no. of even and odd integers
    for (int i = 0; i < size; i++) {
        // If element is odd
        if (arr[i] & 1)
            odd_freq++;
  
        // If element is even
        else
            even_freq++;
    }
  
    // Check if even_freq is more than N
    if (even_freq >= N)
        return true;
    else {
  
        // If odd_freq is odd
        if (odd_freq & 1) {
  
            // Consider even count of odd
            int taken = odd_freq - 1;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req) {
                return false;
            }
            else
                return true;
        }
        else {
  
            int taken = odd_freq;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req) {
                return false;
            }
            else
                return true;
        }
    }
  
    return false;
}
  
// Driver Code
int main()
{
    int arr[] = { 9, 2, 3, 4, 18, 7, 7, 6 };
    int size = sizeof(arr) / sizeof(arr[0]);
    int N = 5;
  
    if (checkEvenSum(arr, N, size))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}


Java
// Java efficient program to check
// if N numbers with Odd sum can be
// selected from the given array
import java.util.*;
  
class GFG{
  
// Function to check if an odd sum can be
// made using N integers from the array
static boolean checkEvenSum(int arr[], 
                            int N, int size)
{
      
    // Initialize odd and even counts
    int even_freq = 0, odd_freq = 0;
  
    // Iterate over the array to count
    // the no. of even and odd integers
    for(int i = 0; i < size; i++)
    {
          
        // If element is odd
        if (arr[i] % 2 == 1)
            odd_freq++;
  
        // If element is even
        else
            even_freq++;
    }
  
    // Check if even_freq is more than N
    if (even_freq >= N)
        return true;
    else
    {
  
        // If odd_freq is odd
        if (odd_freq % 2 == 1) 
        {
  
            // Consider even count of odd
            int taken = odd_freq - 1;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req)
            {
                return false;
            }
            else
                return true;
        }
        else
        {
            int taken = odd_freq;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req) 
            {
                return false;
            }
            else
                return true;
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 9, 2, 3, 4, 18, 7, 7, 6 };
    int size = arr.length;
    int N = 5;
  
    if (checkEvenSum(arr, N, size))
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
}
}
  
// This code is contributed by Rohit_ranjan


Python3
# Python3 efficient program to check
# if N numbers with Odd sum can be
# selected from the given array
  
# Function to check if an odd sum can be
# made using N integers from the array
def checkEvenSum(arr, N, size):
  
    # Initialize odd and even counts
    even_freq , odd_freq = 0 , 0
  
    # Iterate over the array to count
    # the no. of even and odd integers
    for i in range(size):
          
        # If element is odd
        if (arr[i] & 1):
            odd_freq += 1
  
        # If element is even
        else:
            even_freq += 1
      
    # Check if even_freq is more than N
    if (even_freq >= N):
        return True
    else:
  
        # If odd_freq is odd
        if (odd_freq & 1):
  
            # Consider even count of odd
            taken = odd_freq - 1
  
            # Calculate even required
            req = N - taken
  
            # If even count is less
            # than required count
            if (even_freq < req):
                return False
            else:
                return True
  
        else:
            taken = odd_freq
  
            # Calculate even required
            req = N - taken
  
            # If even count is less
            # than required count
            if (even_freq < req):
                return False
            else:
                return True
  
    return False
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 9, 2, 3, 4, 18, 7, 7, 6 ]
    size = len(arr)
    N = 5
  
    if (checkEvenSum(arr, N, size)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by chitranayal


C#
// C# efficient program to check
// if N numbers with Odd sum can be
// selected from the given array
using System;
  
class GFG{
  
// Function to check if an odd sum can be
// made using N integers from the array
static bool checkEvenSum(int []arr, 
                         int N, int size)
{
      
    // Initialize odd and even counts
    int even_freq = 0, odd_freq = 0;
  
    // Iterate over the array to count
    // the no. of even and odd integers
    for(int i = 0; i < size; i++)
    {
          
        // If element is odd
        if (arr[i] % 2 == 1)
            odd_freq++;
  
        // If element is even
        else
            even_freq++;
    }
  
    // Check if even_freq is more than N
    if (even_freq >= N)
        return true;
          
    else
    {
  
        // If odd_freq is odd
        if (odd_freq % 2 == 1) 
        {
  
            // Consider even count of odd
            int taken = odd_freq - 1;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req)
            {
                return false;
            }
            else
                return true;
        }
        else
        {
            int taken = odd_freq;
  
            // Calculate even required
            int req = N - taken;
  
            // If even count is less
            // than required count
            if (even_freq < req) 
            {
                return false;
            }
            else
                return true;
        }
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 9, 2, 3, 4, 18, 7, 7, 6 };
    int size = arr.Length;
    int N = 5;
  
    if (checkEvenSum(arr, N, size))
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
}
}
  
// This code is contributed by Rajput-Ji


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)