From d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa Mon Sep 17 00:00:00 2001 From: jack <0x6A73@pm.me> Date: Fri, 14 Jun 2024 13:13:15 -0600 Subject: initial --- triples_bitwise_and_zero.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 triples_bitwise_and_zero.c (limited to 'triples_bitwise_and_zero.c') 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 +} -- cgit v1.2.3