给定一个由N 个整数组成的整数数组A[] ,找到索引 (i, j, k) 的三元组数,使得A[i] & A[j] & A[k]为0 (<0 ≤ i , j, k ≤ N 和&表示按位与运算符。
例子:
Input: A[]={2, 1, 3}
Output: 12
Explanation: The following i, j, k triples can be chosen whose bitwise AND is zero:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Input: A[]={21, 15, 6}
Output: 0
Explanation: No such triplets exist.
方法:解决这个问题的思路是使用一个Map来处理数组求解元素。请按照以下步骤解决问题:
- 初始化 Map 以存储A[i] 和 A[j]的每个可能值的频率。此外,用0初始化一个变量answer ,以存储所需的计数。
- 遍历数组,对于每个数组元素,遍历映射并检查每个映射是否为键,如果它与当前数组元素的按位 AND是否为 0。对于发现为真的每个数组元素,按关键字的频率增加答案。
- 遍历完数组后,打印answer 。
下面是上述方法的实现:
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
Javascript
12
时间复杂度: O(max(M, N 2 )) 其中M是给定数组中存在的最大元素
辅助空间: O(M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live