📌  相关文章
📜  来自两个数组的不同对总数,因此可以通过反转第一个的位来获得第二个数

📅  最后修改于: 2021-04-29 05:26:14             🧑  作者: Mango

给定两个数组arr1 []arr2 [] ,任务是从第一个数组(例如a)中取一个元素,从第二个数组(例如b)中取一个元素。如果通过反转比特形成的数量等于b,则在一对(A,B)是一个有效的对。

位反转示例:
11以二进制形式写为1011 。将其位反转后,将获得0100 (十进制为4 )。因此(11,4)是有效对,但(4,11)不是,因为将4的数字取反即100-> 0113后无法获得11

例子:

方法:

  • 取两个空集s1s2
  • s2中插入arr2 []的所有元素。
  • 迭代第一个数组。如果在第一组中不存在该元素,而在第二组中存在通过反转其位而形成的数字,则增加计数并将当前元素插入s1中,以便不再进行计数。
  • 最后打印计数值

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the number formed
// by inverting bits the bits of num
int invertBits(int num)
{
    // Number of bits in num
    int x = log2(num) + 1;
  
    // Inverting the bits one by one
    for (int i = 0; i < x; i++)
        num = (num ^ (1 << i));
  
    return num;
}
  
// Function to return the total valid pairs
int totalPairs(int arr1[], int arr2[], int n, int m)
{
  
    // Set to store the elements of the arrays
    unordered_set s1, s2;
  
    // Insert all the elements of arr2[] in the set
    for (int i = 0; i < m; i++)
        s2.insert(arr2[i]);
  
    // Initialize count variable to 0
    int count = 0;
    for (int i = 0; i < n; i++) {
  
        // Check if element of the first array
        // is not in the first set
        if (s1.find(arr1[i]) == s1.end()) {
  
            // Check if the element formed by inverting bits
            // is in the second set
            if (s2.find(invertBits(arr1[i])) != s2.end()) {
  
                // Increment the count of valid pairs and insert
                // the element in the first set so that
                // it doesn't get counted again
                count++;
                s1.insert(arr1[i]);
            }
        }
    }
  
    // Return the total number of pairs
    return count;
}
  
// Driver code
int main()
{
    int arr1[] = { 43, 7, 1, 99 };
    int arr2[] = { 5, 1, 28, 20 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);
  
    cout << totalPairs(arr1, arr2, n, m);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*; 
import java.io.*; 
import java.lang.*; 
  
class GFG 
{ 
  
static int log2(int N) 
{ 
    // calculate log2 N indirectly 
    // using log() method 
    int result = (int)(Math.log(N) / Math.log(2)); 
  
    return result; 
}
  
// Function to return the number formed 
// by inverting bits the bits of num 
static int invertBits(int num) 
{ 
    // Number of bits in num 
    int x = log2(num) + 1; 
  
    // Inverting the bits one by one 
    for (int i = 0; i < x; i++) 
        num = (num ^ (1 << i)); 
  
    return num; 
} 
  
// Function to return the total valid pairs 
static int totalPairs(int arr1[], int arr2[], int n, int m) 
{ 
  
    // Set to store the elements of the arrays 
    HashSet s1 = new HashSet(); 
    HashSet s2 = new HashSet(); 
  
    // add all the elements of arr2[] in the set 
    for (int i = 0; i < m; i++) 
        s2.add(arr2[i]); 
  
    // Initialize count variable to 0 
    int count = 0; 
    for (int i = 0; i < n; i++)
    { 
  
        // Check if element of the first array 
        // is not in the first set 
        if (!s1.contains(arr1[i])) 
        { 
  
            // Check if the element formed by inverting bits 
            // is in the second set 
            if (s2.contains(invertBits(arr1[i])))
            { 
  
                // Increment the count of valid pairs and add 
                // the element in the first set so that 
                // it doesn't get counted again 
                count++; 
                s1.add(arr1[i]); 
            } 
        } 
    } 
  
    // Return the total number of pairs 
    return count; 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int arr1[] = { 43, 7, 1, 99 }; 
    int arr2[] = { 5, 1, 28, 20 }; 
    int n = arr1.length; 
    int m = arr2.length; 
  
    System.out.println(totalPairs(arr1, arr2, n, m)); 
} 
}
  
// This code is contributed by SHUBHAMSINGH10


Python3
# Python3 implementation of the approach 
from math import log2;
  
# Function to return the number formed 
# by inverting bits the bits of num 
def invertBits(num) : 
      
    # Number of bits in num 
    x = log2(num) + 1; 
  
    # Inverting the bits one by one 
    for i in range(int(x)) : 
        num = (num ^ (1 << i)); 
  
    return num; 
  
# Function to return the total valid pairs 
def totalPairs(arr1, arr2, n, m) : 
  
    # Set to store the elements of the arrays 
    s1, s2 = set(), set();
      
    # Insert all the elements of 
    # arr2[] in the set
    for i in range(m) :
        s2.add(arr2[i]);
          
    # Initialize count variable to 0 
    count = 0;
    for i in range(n) :
          
        # Check if element of the first array 
        # is not in the first set
        if arr1[i] not in s1 :
              
            # Check if the element formed by 
            # inverting bits is in the second set 
            if invertBits(arr1[i]) in s2 :
                  
                # Increment the count of valid pairs 
                # and insert the element in the first 
                # set so that it doesn't get counted again 
                count += 1;
                s1.add(arr1[i]);
      
    # Return the total number of pairs
    return count;
  
# Driver code 
if __name__ == "__main__" :
  
    arr1 = [ 43, 7, 1, 99 ]; 
    arr2 = [ 5, 1, 28, 20 ]; 
    n = len(arr1);
    m = len(arr2);
  
    print(totalPairs(arr1, arr2, n, m)); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach 
using System; 
using System.Collections.Generic;
  
class GFG 
{ 
   
static int log2(int N) 
{ 
    // calculate log2 N indirectly 
    // using log() method 
    int result = (int)(Math.Log(N) / Math.Log(2)); 
   
    return result; 
}
   
// Function to return the number formed 
// by inverting bits the bits of num 
static int invertBits(int num) 
{ 
    // Number of bits in num 
    int x = log2(num) + 1; 
   
    // Inverting the bits one by one 
    for (int i = 0; i < x; i++) 
        num = (num ^ (1 << i)); 
   
    return num; 
} 
   
// Function to return the total valid pairs 
static int totalPairs(int []arr1, int []arr2, int n, int m) 
{ 
   
    // Set to store the elements of the arrays 
    HashSet s1 = new HashSet(); 
    HashSet s2 = new HashSet(); 
   
    // add all the elements of arr2[] in the set 
    for (int i = 0; i < m; i++) 
        s2.Add(arr2[i]); 
   
    // Initialize count variable to 0 
    int count = 0; 
    for (int i = 0; i < n; i++)
    { 
   
        // Check if element of the first array 
        // is not in the first set 
        if (!s1.Contains(arr1[i])) 
        { 
   
            // Check if the element formed by inverting bits 
            // is in the second set 
            if (s2.Contains(invertBits(arr1[i])))
            { 
   
                // Increment the count of valid pairs and add 
                // the element in the first set so that 
                // it doesn't get counted again 
                count++; 
                s1.Add(arr1[i]); 
            } 
        } 
    } 
   
    // Return the total number of pairs 
    return count; 
} 
   
// Driver code 
public static void Main() 
{ 
    int []arr1 = { 43, 7, 1, 99 }; 
    int []arr2 = { 5, 1, 28, 20 }; 
    int n = arr1.Length; 
    int m = arr2.Length; 
   
    Console.Write(totalPairs(arr1, arr2, n, m)); 
} 
}
   
// This code is contribute by chitranayal


输出:
2