Check if a subsequence of length K with odd sum exists | Java program to check if a subsequence of length K with odd sum exists in the given array ; Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code
class GFG { static boolean isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }