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

📅  最后修改于: 2021-09-06 06:24:48             🧑  作者: Mango

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

例子:

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

  1. 计算偶数和奇数并将其分别存储在even_freqodd_freq 中
  2. 如果even_freq超过N ,则取所有偶数,它们的总和将是偶数。因此,打印“是”。
  3. 否则,检查odd_freq
  4. 如果odd_freqodd ,则检查(odd_freq + even_freq – 1)是否≥ N。如果发现是真的,打印“”。
  5. 如果odd_freqeven ,则检查(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


Javascript


输出:
Yes

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

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