Length of longest subsequence whose XOR value is odd

Given an array arr[] of N positive integers, the task is to find the length of the longest subsequence such that Bitwise XOR of all integers in the subsequence is odd.

Examples:

Input: N = 7, arr[] = {2, 3, 4, 1, 5, 6, 7}
Output: 6
Explanation: The subsequence of maximum length is {2, 3, 4, 5, 6, 7
with XOR of all elements as 1.
Other subsequences also exists.

Input: N = 4, arr[] = {2, 4, 6, 8}
Output: 0
Explanation: No possible subsequence exits.

 

Naive Approach: The naive idea is to generate all possible subsequence of the given array and check if Bitwise XOR of any subsequence is odd or not. If there exist subsequences whose Bitwise XOR is odd then print the maximum length of that among those subsequences.

Time Complexity: O(N*2N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above naive approach the idea is to count the number of odd and even element in the given array and find the length of the longest subsequence as below:



  1. Count the number of even and odd elements in arr[].
  2. If the count of odd values is equal to array size i.e., N, then we have two possible cases:
    1. If the size of the array is odd then the max length will be equal to N
    2. Else max length will be equal to N – 1.
  3. If the count of even values is equal to array size then the max length will be zero.
  4. Now if both types of elements are present in the given array, then the max length will include all the even elements and for odd elements, we include all of them if the count of odd values is odd else we include odd – 1 elements.
  5. Print the maximum length of the longest subsequence after the above steps.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
     
// Function for find max XOR subsequence
// having odd value
static int maxXORSubsequence(int arr[], int n)
{
     
    // Initialize odd and even count
    int i, odd = 0, even = 0;
 
    // Count the number of odd and even
    // numbers in given array
    for(i = 0; i < n; i++)
    {
        if ((arr[i] & 1) != 0)
            odd++;
        else
            even++;
    }
 
    int maxlen;
 
    if (odd == n)
    {
         
        // If all values are odd
        // in given array
        if (odd % 2 == 0)
            maxlen = n - 1;
        else
            maxlen = n;
    }
    else if (even == n)
    {
 
        // If all values are even
        // in given array
        maxlen = 0;
    }
    else
    {
 
        // If both odd and even are
        // present in given array
        if (odd % 2 == 0)
            maxlen = even + odd - 1;
        else
            maxlen = even + odd;
    }
    return maxlen;
}
 
// Driver Code
public static void main (String []args)
{
     
    // Given array arr[]
    int arr[] = { 2, 3, 4, 5, 6, 7 };
    int n = arr.length;
 
    // Function Call
    System.out.print( maxXORSubsequence(arr, n));
}
}
 
// This code is contributed by chitranayal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function for find max XOR subsequence
# having odd value
def maxXorSubsequence(arr, n):
     
    # Initialize odd and even count
    odd = 0
    even = 0
     
    # Count the number of odd and even
    # numbers in given array
    for i in range(0, n):
        if arr[i] % 2 != 0:
            odd += 1
        else:
            even += 1
    if odd == n:
         
        # If all values are odd
        # in given array
        if odd % 2 == 0:
            maxlen = n - 1
        else:
            maxlen = n
             
    elif even == n:
         
        # If all values are even
        # in given array
        maxlen = 0
    else:
         
        # If both odd and even are
        # present in given array
        if odd % 2 == 0:
            maxlen = even + odd - 1
        else:
            maxlen = even + odd
             
    return maxlen
 
# Driver code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 2, 3, 4, 5, 6, 7 ]
    n = len(arr)
     
    # Function Call
    print(maxXorSubsequence(arr,n))
     
# This code is contributed by virusbuddah_

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
      
// Function for find max XOR subsequence
// having odd value
static int maxXORSubsequence(int[] arr, int n)
{
      
    // Initialize odd and even count
    int i, odd = 0, even = 0;
  
    // Count the number of odd and even
    // numbers in given array
    for(i = 0; i < n; i++)
    {
        if ((arr[i] & 1) != 0)
            odd++;
        else
            even++;
    }
  
    int maxlen;
  
    if (odd == n)
    {
          
        // If all values are odd
        // in given array
        if (odd % 2 == 0)
            maxlen = n - 1;
        else
            maxlen = n;
    }
    else if (even == n)
    {
  
        // If all values are even
        // in given array
        maxlen = 0;
    }
    else
    {
  
        // If both odd and even are
        // present in given array
        if (odd % 2 == 0)
            maxlen = even + odd - 1;
        else
            maxlen = even + odd;
    }
    return maxlen;
}
  
// Driver Code
public static void Main (string []args)
{
      
    // Given array arr[]
    int []arr = { 2, 3, 4, 5, 6, 7 };
    int n = arr.Length;
  
    // Function Call
    Console.Write( maxXORSubsequence(arr, n));
}
}
  
// This code is contributed by rock_cool

chevron_right


Output:

6



Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.