Skip to content

Commit 6ab210d

Browse files
committed
New Problem Solution "982. Triples with Bitwise AND Equal To Zero"
1 parent 4c7451b commit 6ab210d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ LeetCode
2525
|985|[Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | [C++](./algorithms/cpp/sumOfEvenNumbersAfterQueries/SumOfEvenNumbersAfterQueries.cpp)|Easy|
2626
|984|[String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | [C++](./algorithms/cpp/stringWithoutAAAOrBBB/StringWithoutAAAOrBBB.cpp)|Easy|
2727
|983|[Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/) | [C++](./algorithms/cpp/minimumCostForTickets/MinimumCostForTickets.cpp)|Medium|
28+
|982|[Triples with Bitwise AND Equal To Zero](https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/) | [C++](./algorithms/cpp/triplesWithBitwiseANDEqualToZero/TriplesWithBitwiseAndEqualToZero.cpp)|Hard|
2829
|981|[Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [C++](./algorithms/cpp/timeBasedKeyValueStore/TimeBasedKeyValueStore.cpp)|Medium|
2930
|980|[Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [C++](./algorithms/cpp/uniquePaths/UniquePaths.III.cpp)|Hard|
3031
|979|[Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | [C++](./algorithms/cpp/distributeCoinsInBinaryTree/DistributeCoinsInBinaryTree.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Source : https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/
2+
// Author : Hao Chen
3+
// Date : 2020-07-26
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an array of integers A, find the number of triples of indices (i, j, k) such that:
8+
*
9+
* 0 <= i < A.length
10+
* 0 <= j < A.length
11+
* 0 <= k < A.length
12+
* A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
13+
*
14+
* Example 1:
15+
*
16+
* Input: [2,1,3]
17+
* Output: 12
18+
* Explanation: We could choose the following i, j, k triples:
19+
* (i=0, j=0, k=1) : 2 & 2 & 1
20+
* (i=0, j=1, k=0) : 2 & 1 & 2
21+
* (i=0, j=1, k=1) : 2 & 1 & 1
22+
* (i=0, j=1, k=2) : 2 & 1 & 3
23+
* (i=0, j=2, k=1) : 2 & 3 & 1
24+
* (i=1, j=0, k=0) : 1 & 2 & 2
25+
* (i=1, j=0, k=1) : 1 & 2 & 1
26+
* (i=1, j=0, k=2) : 1 & 2 & 3
27+
* (i=1, j=1, k=0) : 1 & 1 & 2
28+
* (i=1, j=2, k=0) : 1 & 3 & 2
29+
* (i=2, j=0, k=1) : 3 & 2 & 1
30+
* (i=2, j=1, k=0) : 3 & 1 & 2
31+
*
32+
* Note:
33+
*
34+
* 1 <= A.length <= 1000
35+
* 0 <= A[i] < 2^16
36+
*
37+
******************************************************************************************************/
38+
39+
class Solution {
40+
public:
41+
int countTriplets(vector<int>& A) {
42+
int n = A.size();
43+
44+
//using a map to aggregate the duplication
45+
unordered_map<int, int> rec;
46+
for (int i=0; i<n; i++) {
47+
for (int j=0; j<n; j++) {
48+
rec[A[i] & A[j]]++;
49+
}
50+
}
51+
52+
int result = 0;
53+
for (auto &r : rec ) {
54+
for (int k=0; k<n; k++) {
55+
if ((r.first & A[k]) == 0) result+=r.second;
56+
}
57+
}
58+
return result;
59+
}
60+
61+
};

0 commit comments

Comments
 (0)