给定一个大小为N的数组arr[] ,任务是用它们的按位异或替换其按位异或为偶数的一对数组元素。尽可能长时间地重复上述步骤。最后,打印对数组执行的此类操作的计数
例子:
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 添加到数组中。重复上述步骤,直到不能再选择对。打印执行的操作计数。
时间复杂度: 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) 。
- 删除上述步骤中形成的对并分别用它们的异或值替换它们,将偶数元素的计数增加 地板(奇数 / 2)。
- 最后,打印可以用偶数异或形成的对的计数为(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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live