aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-06-30 23:56:28 -0600
committerJack Sangdahl <[email protected]>2024-06-30 23:56:28 -0600
commitfce39ef6eb1369b768a5957bf6e3dbed02a331a1 (patch)
tree57f93e2cd2b64535a30d0fb17816a5f19831f583
parent000ebc1d1cfeb1adfcd2c55ced5dc8c33ad7cdb8 (diff)
downloadleetcode-fce39ef6eb1369b768a5957bf6e3dbed02a331a1.tar.gz
leetcode-fce39ef6eb1369b768a5957bf6e3dbed02a331a1.zip
1550. three consecutive odds
-rw-r--r--three_consecutive_odds.c20
-rw-r--r--three_consecutive_odds.py21
2 files changed, 41 insertions, 0 deletions
diff --git a/three_consecutive_odds.c b/three_consecutive_odds.c
new file mode 100644
index 0000000..1f65dd4
--- /dev/null
+++ b/three_consecutive_odds.c
@@ -0,0 +1,20 @@
+// 1550. Three Consecutive Odds
+#include "leetcode.h"
+
+/*
+ * given an integer array 'arr', return true if there are three consecutive odd
+ * numbers in the array. otherwise, return false
+ */
+
+bool threeConsecutiveOdds(int *arr, int arrSize) {
+ int odds = 0;
+ for (int i = 0; i < arrSize && odds < 3; ++i)
+ odds = arr[i] % 2 ? odds + 1 : 0;
+ return odds == 3;
+}
+
+int main() {
+ int a1[] = {2, 6, 4, 1}, a2[] = {1, 2, 34, 3, 4, 5, 7, 23, 12};
+ printf("%d\n", threeConsecutiveOdds(a1, ARRAY_SIZE(a1))); // expect: 0
+ printf("%d\n", threeConsecutiveOdds(a2, ARRAY_SIZE(a2))); // expect: 1
+}
diff --git a/three_consecutive_odds.py b/three_consecutive_odds.py
new file mode 100644
index 0000000..62fafe3
--- /dev/null
+++ b/three_consecutive_odds.py
@@ -0,0 +1,21 @@
+# 1550. Three Consecutive Odds
+
+"""
+given an integer array 'arr', return true if there are three consecutive odd
+numbers in the array. otherwise, return false
+"""
+
+
+class Solution(object):
+ def threeConsecutiveOdds(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: bool
+ """
+ return "111" in "".join([str(i % 2) for i in arr])
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.threeConsecutiveOdds(arr=[2, 6, 4, 1]))
+ print(obj.threeConsecutiveOdds(arr=[1, 2, 34, 3, 4, 5, 7, 23, 12]))