给定N个正整数的数组arr [] ,任务是找到最长子序列的长度,以使子序列中所有整数的按位XOR奇数。
例子:
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.
天真的方法:天真的想法是生成给定数组的所有可能的子序列,并检查任何子序列的按位XOR是否为奇数。如果存在按位异或为奇数的子序列,则在这些子序列中打印该子序列的最大长度。
时间复杂度: O(N * 2 N )
辅助空间: O(1)
高效的方法:为了优化上述朴素的方法,其思想是计算给定数组中奇数和偶数元素的数目,并找到最长子序列的长度,如下所示:
- 计算arr []中偶数和奇数元素的数量。
- 如果奇数值的数量等于数组大小,即N ,那么我们有两种可能的情况:
- 如果数组的大小为奇数,则最大长度将等于N
- 否则,最大长度将等于N – 1 。
- 如果偶数的计数等于数组大小,则最大长度将为零。
- 现在,如果给定数组中同时存在两种类型的元素,则最大长度将包括所有偶数元素,对于奇数元素,如果奇数值的数量为奇数,则我们将包括所有所有元素,否则,将包括奇数– 1个元素。
- 完成上述步骤后,打印最长子序列的最大长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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);
}
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
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
输出:
6
时间复杂度: O(N)
辅助空间: O(1)