Length of longest subsequence whose XOR value is odd | C ++ program for the above approach ; Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; if all values are odd in given array ; if all values are even in given array ; if both odd and even are present in given array ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE using namespace std ; int maxXORSubsequence ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; else even ++ ; } int maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxXORSubsequence ( arr , n ) ; }