📌  相关文章
📜  通过用它们的按位异或替换这些对来最大化按位异或为偶数的对的计数

📅  最后修改于: 2021-09-02 06:00:22             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是用它们的按位异或替换其按位异或为偶数的一对数组元素。尽可能长时间地重复上述步骤。最后,打印对数组执行的此类操作的计数

例子:

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

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

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

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