给定一个由N 个正整数组成的数组arr[] ,任务是找到最长子序列的长度,使得子序列中所有整数的按位异或为奇数。
例子:
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.
朴素的方法:朴素的想法是生成给定数组的所有可能子序列,并检查任何子序列的按位异或是否为奇数。如果存在按位异或为奇数的子序列,则打印这些子序列之间的最大长度。
时间复杂度: 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live