📌  相关文章
📜  用按位AND等于0来计数三倍

📅  最后修改于: 2021-05-25 04:44:19             🧑  作者: Mango

给定一个由N个整数组成的整数数组A [] ,找到索引(i,j,k)的三元组数,以使A [i]&A [j]&A [k]0 (<0≤i ,j,k≤N并且表示按位AND运算符。

例子:

方法:解决此问题的想法是使用Map处理数组元素。请按照以下步骤解决问题:

  • 初始化映射以存储A [i]和A [j]的每个可能值的频率。另外,用0初始化变量answer ,以存储所需的计数。
  • 遍历数组,对于每个数组元素,遍历该映射,并检查每个映射是否为键,以及与当前数组元素的按位与是否为0。对于被发现为真的每个数组元素,请按键的频率增加答案
  • 遍历数组后,打印答案

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
using namespace std;
 
// Function to find the number of
// triplets whose Bitwise AND is 0.
int countTriplets(vector& A)
{
    // Stores the count of triplets
    // having bitwise AND equal to 0
    int cnt = 0;
 
    // Stores frequencies of all possible A[i] & A[j]
    unordered_map tuples;
 
    // Traverse the array
    for (auto a : A)
 
        // Update frequency of Bitwise AND
        // of all array elements with a
        for (auto b : A)
            ++tuples[a & b];
 
    // Traverse the array
    for (auto a : A)
 
        // Iterate the map
        for (auto t : tuples)
 
            // If bitwise AND of triplet
            // is zero, increment cnt
            if ((t.first & a) == 0)
                cnt += t.second;
 
    // Return the number of triplets
    // whose Bitwise AND is 0.
    return cnt;
}
 
// Driver Code
int main()
{
 
    // Input Array
    vector A = { 2, 1, 3 };
 
    // Function Call
    cout << countTriplets(A);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the number of
// triplets whose Bitwise AND is 0.
static int countTriplets(int []A)
{
   
    // Stores the count of triplets
    // having bitwise AND equal to 0
    int cnt = 0;
 
    // Stores frequencies of all possible A[i] & A[j]
    HashMap tuples = new HashMap();
 
    // Traverse the array
    for (int a : A)
 
        // Update frequency of Bitwise AND
        // of all array elements with a
        for (int b : A)
        {
            if(tuples.containsKey(a & b))
                tuples.put(a & b, tuples.get(a & b) + 1);
            else
                tuples.put(a & b, 1);
        }
 
    // Traverse the array
    for (int a : A)
 
        // Iterate the map
        for (Map.Entry t : tuples.entrySet())
 
            // If bitwise AND of triplet
            // is zero, increment cnt
            if ((t.getKey() & a) == 0)
                cnt += t.getValue();
 
    // Return the number of triplets
    // whose Bitwise AND is 0.
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Input Array
    int []A = { 2, 1, 3 };
 
    // Function Call
    System.out.print(countTriplets(A));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find the number of
# triplets whose Bitwise AND is 0.
def countTriplets(A) :
 
    # Stores the count of triplets
    # having bitwise AND equal to 0
    cnt = 0;
 
    # Stores frequencies of all possible A[i] & A[j]
    tuples = {};
 
    # Traverse the array
    for a in A:
 
        # Update frequency of Bitwise AND
        # of all array elements with a
        for b in A:
            if (a & b) in tuples:
                tuples[a & b] += 1;               
            else:
                tuples[a & b] = 1;
 
    # Traverse the array
    for a in A:
         
        # Iterate the map
        for t in tuples:
 
            # If bitwise AND of triplet
            # is zero, increment cnt
            if ((t & a) == 0):
                cnt += tuples[t];
 
    # Return the number of triplets
    # whose Bitwise AND is 0.
    return cnt;
 
# Driver Code
if __name__ ==  "__main__" :
 
    # Input Array
    A = [ 2, 1, 3 ];
 
    # Function Call
    print(countTriplets(A));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to find the number of
// triplets whose Bitwise AND is 0.
static int countTriplets(int []A)
{
   
    // Stores the count of triplets
    // having bitwise AND equal to 0
    int cnt = 0;
 
    // Stores frequencies of all possible A[i] & A[j]
    Dictionary tuples = new Dictionary();
 
    // Traverse the array
    foreach (int a in A)
 
        // Update frequency of Bitwise AND
        // of all array elements with a
        foreach (int b in A)
        {
            if(tuples.ContainsKey(a & b))
                tuples[a & b] = tuples[a & b] + 1;
            else
                tuples.Add(a & b, 1);
        }
 
    // Traverse the array
    foreach (int a in A)
 
        // Iterate the map
        foreach (KeyValuePair t in tuples)
 
            // If bitwise AND of triplet
            // is zero, increment cnt
            if ((t.Key & a) == 0)
                cnt += t.Value;
 
    // Return the number of triplets
    // whose Bitwise AND is 0.
    return cnt;
}
 
// Driver Code
public static void Main(String[] args)
{
 
    // Input Array
    int []A = { 2, 1, 3 };
 
    // Function Call
    Console.Write(countTriplets(A));
}
}
 
// This code is contributed by 29AjayKumar


输出:
12

时间复杂度: O(max(M,N 2 ))其中M是给定数组中存在的最大元素
辅助空间: O(M)