forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriplesWithBitwiseAndEqualToZero.cpp
61 lines (56 loc) · 1.62 KB
/
TriplesWithBitwiseAndEqualToZero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Source : https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/
// Author : Hao Chen
// Date : 2020-07-26
/*****************************************************************************************************
*
* Given an array of integers A, find the number of triples of indices (i, j, k) such that:
*
* 0 <= i < A.length
* 0 <= j < A.length
* 0 <= k < A.length
* A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
*
* Example 1:
*
* Input: [2,1,3]
* Output: 12
* Explanation: We could choose the following i, j, k triples:
* (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
*
* Note:
*
* 1 <= A.length <= 1000
* 0 <= A[i] < 2^16
*
******************************************************************************************************/
class Solution {
public:
int countTriplets(vector<int>& A) {
int n = A.size();
//using a map to aggregate the duplication
unordered_map<int, int> rec;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
rec[A[i] & A[j]]++;
}
}
int result = 0;
for (auto &r : rec ) {
for (int k=0; k<n; k++) {
if ((r.first & A[k]) == 0) result+=r.second;
}
}
return result;
}
};