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:6Explanation: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:0Explanation: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*2 ^{N})*

**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:

- Count the number of even and odd elements in
**arr[]**. - If the count of odd values is equal to array size i.e.,
**N**, then we have two possible cases:- If the size of the array is odd then the max length will be equal to
**N** - Else max length will be equal to
**N – 1**.

- If the size of the array is odd then the max length will be equal to
- If the count of even values is equal to array size then the max length will be zero.
- 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. - 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` `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*

*filter_none*

## Java

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

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Count all pairs of adjacent nodes whose XOR is an odd number
- Longest subsequence whose average is less than K
- Longest subsequence whose sum is divisible by a given number
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest Increasing Odd Even Subsequence
- Count of K length subsequence whose product is even
- Check if a subsequence of length K with odd sum exists
- Count smaller numbers whose XOR with n produces greater value
- Range Query on array whose each element is XOR of index value and previous element
- Find the node whose xor with x gives maximum value
- Find the node whose xor with x gives minimum value
- Find a value whose XOR with given number is maximum
- Length of Longest Balanced Subsequence
- Minimum cost to make Longest Common Subsequence of length k
- Length of longest common subsequence containing vowels
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of Longest Prime Subsequence in an Array
- Length of longest subsequence of Fibonacci Numbers in an Array

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.