Check if N numbers with Even Sum can be selected from a given Array | C ++ 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 ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEvenSum ( int arr [ ] , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] & 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq & 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } return false ; } 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 ; }