aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--special_array.c23
-rw-r--r--special_array.py27
2 files changed, 50 insertions, 0 deletions
diff --git a/special_array.c b/special_array.c
new file mode 100644
index 0000000..977638e
--- /dev/null
+++ b/special_array.c
@@ -0,0 +1,23 @@
+// 3151. Special Array I
+#include "leetcode.h"
+
+/*
+ * an array is considered special if every pair of its adjacent elements
+ * contains two numbers with different parity. you are given an array of
+ * integers 'nums'. return 'true' if 'nums' is a special array, otherwise return
+ * 'false'.
+ */
+
+bool isArraySpecial(int *nums, int numsSize) {
+ for (int i = 0; i < numsSize; i++)
+ if (!(nums[i - 1] & 1) ^ (nums[i] & 1))
+ return false;
+ return true;
+}
+
+int main() {
+ int n1[] = {1}, n2[] = {2, 1, 4}, n3[] = {4, 3, 1, 6};
+ printf("%d\n", isArraySpecial(n1, ARRAY_SIZE(n1))); // expect: 1
+ printf("%d\n", isArraySpecial(n2, ARRAY_SIZE(n2))); // expect: 1
+ printf("%d\n", isArraySpecial(n3, ARRAY_SIZE(n3))); // expect: 0
+}
diff --git a/special_array.py b/special_array.py
new file mode 100644
index 0000000..43f1c5c
--- /dev/null
+++ b/special_array.py
@@ -0,0 +1,27 @@
+# 3151. Special Array I
+
+"""
+an array is considered special if every pair of its adjacent elements
+contains two numbers with different parity. you are given an array of
+integers 'nums'. return 'true' if 'nums' is a special array, otherwise return
+'false'.
+"""
+
+
+class Solution(object):
+ def isArraySpecial(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: bool
+ """
+ for i in range(1, len(nums)):
+ if (nums[i - 1] & 1) ^ (nums[i] & 1) == 0:
+ return False
+ return True
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isArraySpecial(nums=[1]))
+ print(obj.isArraySpecial(nums=[2, 1, 4]))
+ print(obj.isArraySpecial(nums=[4, 3, 1, 6]))