给定的大小为N的阵列ARR [],任务是替换对的数组元素,其按位XOR甚至可以通过位异或的。尽可能重复上述步骤。最后,打印在数组上执行的此类操作的计数
例子:
Input: arr[] = { 4, 6, 1, 3 }
Output: 3
Explanation:
Step 1: Remove the pair (4, 6) and replace them by their XOR value (= 2) in the array. Therefore, the array arr[] modifies to {2, 1, 3}.
Step 2: Remove the pair (1, 3) and replace them by their XOR value (= 2) in the array, modifies the array as arr[] = {2, 2}.
At last select the pair (2, 2) and then remove the pair and insert the xor of 2 and 2 in the array which modifies the array as arr[] ={0}.
Now no other pair can be chosen therefore 3 is the maximum number of pairs whose Xor is even.
Input: arr[ ] = { 1, 2, 3, 4, 5 }
Output: 3
天真的方法:解决此问题的最简单方法是找到阵列中所有可能的对,并检查每对对的按位XOR是否为偶数。如果发现为真,则增加对的数量并从数组中删除两个元素,并将它们的XOR添加到数组中。重复上述步骤,直到无法选择更多对。打印执行的操作数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- Even ^ Even = Even
- Odd ^ Odd = Even
- The total number of pairs that can be formed from only odd numbers satisfying the conditions is odd / 2.
- The total numbers of pairs that can be formed from only even numbers satisfying the conditions is even – 1.
请按照以下步骤解决问题:
- 遍历数组。
- 计算奇数的频率并将其存储在一个变量中,例如odd 。
- 可以由所有奇数数组元素形成的具有偶数XOR的对总数为floor(odd / 2) 。
- 在上述步骤中删除形成的对,并分别用它们的XOR值替换,将偶数元素的数量增加 地板(奇数/ 2)。
- 最后,将偶数XOR可以形成的对数打印为(N –奇数+奇数/ 2 -1)+奇数/ 2。
下面是上述方法的实现:
C++
// C++ program to implement the above approach
#include
using namespace std;
// Function to maximize the count
// of pairs with even XOR possible
// in an array by given operations
int countPairs(int arr[], int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If arr[i] is odd
if (arr[i] & 1)
odd++;
}
// Stores the total number
// of even pairs
int ans = (N - odd + odd / 2
- 1)
+ odd / 2;
return ans;
}
// Driver Code
int main()
{
// Input
int arr[] = { 4, 6, 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to count the number
// of pairs whose XOR is even
cout << countPairs(arr, N);
return 0;
}
Java
// Java program to implement the above approach
public class GFG
{
// Function to maximize the count
// of pairs with even XOR possible
// in an array by given operations
static int countPairs(int []arr, int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// If arr[i] is odd
if ((arr[i] & 1)!=0)
odd++;
}
// Stores the total number
// of even pairs
int ans = (N - odd + odd / 2
- 1)
+ odd / 2;
return ans;
}
// Driver Code
public static void main(String args[])
{
// Input
int []arr = { 4, 6, 1, 3 };
int N = arr.length;
// Function call to count the number
// of pairs whose XOR is even
System.out.println(countPairs(arr, N));
}
}
// This code is contributed by AnkThon.
Python3
# Python3 program to implement the above approach
# Function to maximize the count
# of pairs with even XOR possible
# in an array by given operations
def countPairs(arr, N):
# Stores count of odd
# array elements
odd = 0
# Traverse the array
for i in range(N):
# If arr[i] is odd
if (arr[i] & 1):
odd += 1
# Stores the total number
# of even pairs
ans = (N - odd + odd // 2 - 1) + odd // 2
return ans
# Driver Code
if __name__ == '__main__':
# Input
arr =[4, 6, 1, 3]
N = len(arr)
# Function call to count the number
# of pairs whose XOR is even
print (countPairs(arr, N))
# This code is contributed by mohit kumar 29.
C#
// C# program to implement the above approach
using System;
class GFG
{
// Function to maximize the count
// of pairs with even XOR possible
// in an array by given operations
static int countPairs(int []arr, int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// If arr[i] is odd
if ((arr[i] & 1)!=0)
odd++;
}
// Stores the total number
// of even pairs
int ans = (N - odd + odd / 2
- 1)
+ odd / 2;
return ans;
}
// Driver Code
public static void Main()
{
// Input
int []arr = { 4, 6, 1, 3 };
int N = arr.Length;
// Function call to count the number
// of pairs whose XOR is even
Console.Write(countPairs(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)