Skip to content
Related Articles

Related Articles

Improve Article
Check if N numbers with Even Sum can be selected from a given Array
  • Difficulty Level : Easy
  • Last Updated : 04 May, 2021

Given an array arr[] and an odd integer N, the task is to check if N numbers can be selected from the array having even sum. Print Yes if possible. Otherwise print No.

Examples:

Input: arr[] = {9, 2, 3, 4, 1, 8, 7, 7, 6}, N = 5 
Output: Yes 
Explanation: {9, 3, 1, 7, 6} are the N elements having even sum

Input: arr[] = {1, 3, 7, 9, 3}, N = 3 
Output: No

Approach: Follow the steps below to solve the problem:



  1. Count even and odd integers and store it in even_freq and odd_freq respectively.
  2. If even_freq exceeds N, then take all even numbers and their sum will be even. Therefore, print “Yes”.
  3. Otherwise, check for odd_freq.
  4. If odd_freq is odd, then check if (odd_freq + even_freq – 1) is ≥ N or not. If found to be true, print “Yes“.
  5. If odd_freq is even, then then check if (odd_freq + even_freq) is ≥ N or not. If found to be true, print “Yes“.
  6. If none of the above conditions satisfy, print “No”.

Below is the implementation of the above approach:

C++




// C++ efficient program to check
// if N numbers with Odd sum can be
// selected from the given array
#include <bits/stdc++.h>
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




<script>
 
// Javascript 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
function checkEvenSum(arr, N, size)
{
     
    // Initialize odd and even counts
    var even_freq = 0, odd_freq = 0;
 
    // Iterate over the array to count
    // the no. of even and odd integers
    for(var 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
            var taken = odd_freq - 1;
 
            // Calculate even required
            var req = N - taken;
 
            // If even count is less
            // than required count
            if (even_freq < req)
            {
                return false;
            }
            else
                return true;
        }
        else
        {
            var taken = odd_freq;
 
            // Calculate even required
            var req = N - taken;
 
            // If even count is less
            // than required count
            if (even_freq < req)
            {
                return false;
            }
            else
                return true;
        }
    }
    return false;
}
 
// Driver Code
var arr = [ 9, 2, 3, 4, 18, 7, 7, 6 ];
var size = arr.length;
var N = 5;
 
if (checkEvenSum(arr, N, size))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by rrrtnx
 
</script>
Output: 
Yes

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :