给定一个由N个正整数组成的数组arr [] ,将其按位与超过按位XOR值的成对数组元素替换为其按位与值。最后,计算可以从数组中生成的此类对的最大数量。
例子:
Input: arr[] = {12, 9, 15, 7}
Output: 2
Explanation:
Step 1: Select the pair {12, 15} and replace the pair by their Bitwise AND (= 12). The array arr[] modifies to {12, 9, 7}.
Step 2: Replace the pair {12, 9} by their Bitwise AND (= 8). Therefore, the array arr[] modifies to {8, 7}.
Therefore, the maximum number of such pairs is 2.
Input: arr[] = {2, 6, 12, 18, 9}
Output: 1
天真的方法:解决此问题的最简单方法是生成所有可能的对,并选择一个具有按位AND大于其按位XOR的对。替换成对,然后插入其按位AND 。重复上述过程,直到没有找到这样的对。打印获得的此类对的计数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- 在第i个位置具有最高有效位的数字只能与第i个位置具有MSB的其他数字形成一对。
- 如果选择了这些对中的一对,则它们的MSB位于第i位的总数减少了1。
- 因此,可以在第i个位置上形成的总对是具有MB编号的在i中的总计数个位置下降1。
请按照以下步骤解决问题:
- 初始化一个映射,例如freq ,以存储在各个位位置处具有MSB的数目的计数。
- 遍历数组,对于每个数组元素arr [i] ,找到arr [i]的最高有效位,然后将频率中最高有效位的计数增加1 。
- 初始化一个变量,例如Pairs ,以存储总对数。
- 遍历地图并将更新对成对+ =(freq [i] – 1) 。
- 完成上述步骤后,打印成对的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// pairs whose Bitwise AND is
// greater than the Bitwise XOR
int countPairs(int arr[], int N)
{
// Stores the frequency of
// MSB of array elements
unordered_map freq;
// Traverse the array
for (int i = 0; i < N; i++) {
// Increment count of numbers
// having MSB at log(arr[i])
freq[log2(arr[i])]++;
}
// Stores total number of pairs
int pairs = 0;
// Traverse the Map
for (auto i : freq) {
pairs += i.second - 1;
}
// Return total count of pairs
return pairs;
}
// Driver Code
int main()
{
int arr[] = { 12, 9, 15, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << countPairs(arr, N);
return 0;
}
Java
// C# program for the above approach
import java.util.*;
class GFG {
// Function to count the number of
// pairs whose Bitwise AND is
// greater than the Bitwise XOR
static int countPairs(int[] arr, int N)
{
// Stores the frequency of
// MSB of array elements
HashMap freq
= new HashMap();
// Traverse the array
for (int i = 0; i < N; i++) {
// Increment count of numbers
// having MSB at log(arr[i])
if (freq.containsKey((int)(Math.log(arr[i]))))
freq.put((int)(Math.log(arr[i])),
(int)(Math.log(arr[i])) + 1);
else
freq.put((int)(Math.log(arr[i])), 1);
}
// Stores total number of pairs
int pairs = 0;
// Traverse the Map
for (Map.Entry item :
freq.entrySet())
{
pairs += item.getValue() - 1;
}
// Return total count of pairs
return pairs;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 12, 9, 15, 7 };
int N = arr.length;
System.out.println(countPairs(arr, N));
}
}
// This code is contributed by ukasp.
Python3
# Python3 program for the above approach
from math import log2
# Function to count the number of
# pairs whose Bitwise AND is
# greater than the Bitwise XOR
def countPairs(arr, N):
# Stores the frequency of
# MSB of array elements
freq = {}
# Traverse the array
for i in range(N):
# Increment count of numbers
# having MSB at log(arr[i])
x = int(log2(arr[i]))
freq[x] = freq.get(x, 0) + 1
# Stores total number of pairs
pairs = 0
# Traverse the Map
for i in freq:
pairs += freq[i] - 1
# Return total count of pairs
return pairs
# Driver Code
if __name__ == '__main__':
arr = [12, 9, 15, 7]
N = len(arr)
print(countPairs(arr, N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to count the number of
// pairs whose Bitwise AND is
// greater than the Bitwise XOR
static int countPairs(int []arr, int N)
{
// Stores the frequency of
// MSB of array elements
Dictionary freq = new Dictionary();
// Traverse the array
for (int i = 0; i < N; i++)
{
// Increment count of numbers
// having MSB at log(arr[i])
if(freq.ContainsKey((int)(Math.Log(Convert.ToDouble(arr[i]),2.0))))
freq[(int)(Math.Log(Convert.ToDouble(arr[i]),2.0))]++;
else
freq[(int)(Math.Log(Convert.ToDouble(arr[i]),2.0))] = 1;
}
// Stores total number of pairs
int pairs = 0;
// Traverse the Map
foreach(var item in freq)
{
pairs += item.Value - 1;
}
// Return total count of pairs
return pairs;
}
// Driver Code
public static void Main()
{
int []arr = { 12, 9, 15, 7 };
int N = arr.Length;
Console.WriteLine(countPairs(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
2
时间复杂度: O(N)
辅助空间: O(32)