Open In App
Related Articles

Length of longest subsequence whose XOR value is odd

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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++

// 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 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
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);
}

                    

Java

// Java program for the above approach
import java.io.*;
 
public 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

                    

Python3

# 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_

                    

C#

// 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

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function for find max XOR subsequence
// having odd value
function maxXORSubsequence(arr, n)
{
     
    // Initialize odd and even count
    let odd = 0, even = 0;
 
    // Count the number of odd and even
    // numbers in given array
    for(let i = 0; i < n; i++)
    {
        if ((arr[i] & 1) != 0)
            odd++;
        else
            even++;
    }
 
    let 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
 
// Given array arr[]
let arr = [ 2, 3, 4, 5, 6, 7 ];
let n = arr.length;
 
// Function Call
document.write(maxXORSubsequence(arr, n));
 
// This code is contributed by divyesh072019
     
</script>

                    

Output:
6

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



Last Updated : 09 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads