Check if sum of exactly K elements of the Array can be odd or not | Java 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
class GFG { static boolean 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 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 8 } ; int K = 5 ; int N = arr . length ; if ( isPossible ( arr , N , K ) ) { System . out . println ( " Possible " ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } }