Check if K distinct array elements form an odd sum | C ++ program for above approach ; Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool oddSum ( vector < int > & A , int N , int K ) { set < int > Odd ; set < int > Even ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . insert ( A [ i ] ) ; } else { Odd . insert ( A [ i ] ) ; } } if ( Odd . size ( ) >= K ) return true ; bool flag = false ; for ( int i = 1 ; i < K ; i += 2 ) { int needed = K - i ; if ( needed <= Even . size ( ) ) { return true ; } } return flag ; } int main ( ) { int K = 5 ; vector < int > A = { 12 , 1 , 7 , 7 , 26 , 18 } ; int N = 3 ; if ( oddSum ( A , N , K ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }