Check if sum of exactly K elements of the Array can be odd or not | C ++ implementation of the above approach ; Function returns true if it is possible to have odd sum ; counting number of odd and even elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int N , int K ) { int oddCount = 0 , evenCount = 0 ; 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 ; } 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 ; }