Length of longest subsequence whose XOR value is odd
Last Updated :
09 Dec, 2022
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:
- 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 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++
#include <iostream>
using namespace std;
int maxXORSubsequence( int arr[], int n)
{
int i, odd = 0, even = 0;
for (i = 0; i < n; i++) {
if ((arr[i] & 1) != 0)
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;
}
return maxlen;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxXORSubsequence(arr, n);
}
|
Java
import java.io.*;
public class GFG {
static int maxXORSubsequence( int arr[], int n)
{
int i, odd = 0 , even = 0 ;
for (i = 0 ; i < n; i++) {
if ((arr[i] & 1 ) != 0 )
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;
}
return maxlen;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 5 , 6 , 7 };
int n = arr.length;
System.out.print(maxXORSubsequence(arr, n));
}
}
|
Python3
def maxXorSubsequence(arr, n):
odd = 0
even = 0
for i in range ( 0 , n):
if arr[i] % 2 ! = 0 :
odd + = 1
else :
even + = 1
if odd = = n:
if odd % 2 = = 0 :
maxlen = n - 1
else :
maxlen = n
elif even = = n:
maxlen = 0
else :
if odd % 2 = = 0 :
maxlen = even + odd - 1
else :
maxlen = even + odd
return maxlen
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 , 6 , 7 ]
n = len (arr)
print (maxXorSubsequence(arr,n))
|
C#
using System;
class GFG{
static int maxXORSubsequence( int [] arr, int n)
{
int i, odd = 0, even = 0;
for (i = 0; i < n; i++)
{
if ((arr[i] & 1) != 0)
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;
}
return maxlen;
}
public static void Main ( string []args)
{
int []arr = { 2, 3, 4, 5, 6, 7 };
int n = arr.Length;
Console.Write( maxXORSubsequence(arr, n));
}
}
|
Javascript
<script>
function maxXORSubsequence(arr, n)
{
let odd = 0, even = 0;
for (let i = 0; i < n; i++)
{
if ((arr[i] & 1) != 0)
odd++;
else
even++;
}
let 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;
}
return maxlen;
}
let arr = [ 2, 3, 4, 5, 6, 7 ];
let n = arr.length;
document.write(maxXORSubsequence(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...