aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-16 23:10:20 -0600
committerJack Sangdahl <[email protected]>2025-03-16 23:10:20 -0600
commit5b627305f75df05f28670508f2e7b30f094000d0 (patch)
tree58cc8c12722bd807188706e16ce764dde8ff7b0d
parent27d29594b7a1211d863f38133148842a3285d9e0 (diff)
downloadleetcode-5b627305f75df05f28670508f2e7b30f094000d0.tar.gz
leetcode-5b627305f75df05f28670508f2e7b30f094000d0.zip
2206. divide array into equal pairs
-rw-r--r--divide_array_equal_pair.c24
-rw-r--r--divide_array_equal_pair.py26
2 files changed, 50 insertions, 0 deletions
diff --git a/divide_array_equal_pair.c b/divide_array_equal_pair.c
new file mode 100644
index 0000000..cc98130
--- /dev/null
+++ b/divide_array_equal_pair.c
@@ -0,0 +1,24 @@
+// 2206. Divide Array Into Equal Pairs
+#include "leetcode.h"
+
+/*
+ * you are given an integer array 'nums' consisting of '2 * n' integers. you
+ * need to divide 'nums' into 'n' pairs such that: each element belongs to
+ * exactly one pair, and the elements present in a pair are equal. return true
+ * if 'nums' can be divided into 'n' pairs, otherwise return false.
+ */
+
+bool divideArray(int *nums, int numsSize) {
+ int x1 = 0, x2 = 0;
+ for (int i = 0; i < numsSize; i++) {
+ x1 ^= nums[i];
+ x2 ^= nums[i] + 1;
+ }
+ return !x1 && !x2;
+}
+
+int main() {
+ int n1[] = {3, 2, 3, 2, 2, 2}, n2[] = {1, 2, 3, 4};
+ printf("%d\n", divideArray(n1, ARRAY_SIZE(n1))); // expect: 1
+ printf("%d\n", divideArray(n2, ARRAY_SIZE(n2))); // expect: 0
+}
diff --git a/divide_array_equal_pair.py b/divide_array_equal_pair.py
new file mode 100644
index 0000000..7555627
--- /dev/null
+++ b/divide_array_equal_pair.py
@@ -0,0 +1,26 @@
+# 2206. Divide Array Into Equal Pairs
+
+"""
+you are given an integer array 'nums' consisting of '2 n' integers. you
+need to divide 'nums' into 'n' pairs such that: each element belongs to
+exactly one pair, and the elements present in a pair are equal. return true
+if 'nums' can be divided into 'n' pairs, otherwise return false.
+"""
+
+
+class Solution(object):
+ def divideArray(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: bool
+ """
+ vis = set()
+ for i in nums:
+ vis ^= {i}
+ return not vis
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.divideArray(nums=[3, 2, 3, 2, 2, 2]))
+ print(obj.divideArray(nums=[1, 2, 3, 4]))