aboutsummaryrefslogtreecommitdiff
path: root/count_triplets_form_arr_xor.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_triplets_form_arr_xor.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'count_triplets_form_arr_xor.c')
-rw-r--r--count_triplets_form_arr_xor.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/count_triplets_form_arr_xor.c b/count_triplets_form_arr_xor.c
new file mode 100644
index 0000000..cfb7493
--- /dev/null
+++ b/count_triplets_form_arr_xor.c
@@ -0,0 +1,31 @@
+// 1442. Count Triplets That Can Form Two Arrays of Equal XOR
+#include "leetcode.h"
+
+/*
+ * given an array of integers 'arr'. we want to select three indices 'i', 'j',
+ * and 'k' where '(0 <= i < j <= k < arr.length)'. lets define 'a' and 'b' as
+ * follows. 'a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]', 'b = arr[j] ^ arr[j +
+ * 1] ^ ... ^ arr[k]'. note that '^' denotes the bitwise xor operation. return
+ * the number of triplets (i, j, and k) where 'a == b'
+ */
+
+int countTriplets(int *arr, int arrSize) {
+ if (arrSize < 2)
+ return 0;
+ int ans = 0;
+ for (int i = 0; i < arrSize; i++) {
+ int tmp = arr[i];
+ for (int j = i + 1; j < arrSize; j++) {
+ tmp ^= arr[j];
+ if (!tmp)
+ ans += j - i;
+ }
+ }
+ return ans;
+}
+
+int main() {
+ int a1[] = {2, 3, 1, 6, 7}, a2[] = {1, 1, 1, 1, 1};
+ printf("%d\n", countTriplets(a1, ARRAY_SIZE(a1))); // expect: 4
+ printf("%d\n", countTriplets(a2, ARRAY_SIZE(a2))); // expect: 10
+}