diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /triples_bitwise_and_zero.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'triples_bitwise_and_zero.c')
-rw-r--r-- | triples_bitwise_and_zero.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/triples_bitwise_and_zero.c b/triples_bitwise_and_zero.c new file mode 100644 index 0000000..c094980 --- /dev/null +++ b/triples_bitwise_and_zero.c @@ -0,0 +1,30 @@ +// 982. Triples with Bitwise AND Equal To Zero +#include "leetcode.h" + +/* + * given an integer array nums, return the number of AND triplets. an AND triple + * is a triple of indices '(i, j, k)' such that '0 <= i < nums.length', '0 <= j + * < nums.length', '0 <= k < nums.length', 'nums[i] & nums[j] & nums[k] == 0' + * where '&' represents the bitwise AND operator + */ + +int countTriplets(int *nums, int nums_size) { + int ans = 0, n = nums_size; + int *filled = calloc(n, sizeof(int)); + for (int i = 0; i < n; i++) { + filled[nums[i]]++; + int cnt = 0; + for (int j = 0; j <= i; j++) + cnt += filled[j]; + if (cnt == i + 1) + ans++; + } + free(filled); + return ans; +} + +int main() { + int n1[] = {2, 1, 3}, n2[] = {0, 0, 0}; + printf("%d\n", countTriplets(n1, ARRAY_SIZE(n1))); // expect: 12 + printf("%d\n", countTriplets(n2, ARRAY_SIZE(n2))); // expect: 27 +} |