📌  相关文章
📜  通过将按位异或替换为按对异或的对来最大化其对的数量

📅  最后修改于: 2021-05-13 22:21:04             🧑  作者: Mango

给定的大小为N的阵列ARR [],任务是替换对的数组元素,其按位XOR甚至可以通过位异或的。尽可能重复上述步骤。最后,打印在数组上执行的此类操作的计数

例子:

天真的方法:解决此问题的最简单方法是找到阵列中所有可能的对,并检查每对对的按位XOR是否为偶数。如果发现为真,则增加对的数量并从数组中删除两个元素,并将它们的XOR添加到数组中。重复上述步骤,直到无法选择更多对。打印执行的操作数。
时间复杂度: O(N 3 )
辅助空间: O(1)

高效的方法:可以基于以下观察来优化上述方法:

请按照以下步骤解决问题:

  1. 遍历数组。
  2. 计算奇数的频率并将其存储在一个变量中,例如odd
  3. 可以由所有奇数数组元素形成的具有偶数XOR的对总数为floor(odd / 2)
  4. 在上述步骤中删除形成的对,并分别用它们的XOR值替换,将偶数元素的数量增加 地板(奇数/ 2)。
  5. 最后,将偶数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)