aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--check_n_double_exist.c26
-rw-r--r--check_n_double_exist.py26
2 files changed, 52 insertions, 0 deletions
diff --git a/check_n_double_exist.c b/check_n_double_exist.c
new file mode 100644
index 0000000..5f6d51d
--- /dev/null
+++ b/check_n_double_exist.c
@@ -0,0 +1,26 @@
+// 1346. Check If N and Its Double Exist
+
+#include "leetcode.h"
+
+/*
+ * given an array 'arr' of integers, check if there exists two indices 'i' and
+ * 'j' such that 'i != j', '0 <= i, j < arr.length', and 'arr[i] == 2 * arr[j]'
+ */
+
+bool checkIfExist(int *arr, int arrSize) {
+ for (int i = 0; i < arrSize; i++) {
+ int n = arr[i];
+ for (int j = i + 1; j < arrSize; j++) {
+ int m = arr[j];
+ if (n == (2 * m) || m == (2 * n))
+ return true;
+ }
+ }
+ return false;
+}
+
+int main() {
+ int a1[] = {10, 2, 5, 3}, a2[] = {3, 1, 7, 11};
+ printf("%d\n", checkIfExist(a1, ARRAY_SIZE(a1))); // expect: 1
+ printf("%d\n", checkIfExist(a2, ARRAY_SIZE(a2))); // expect: 0
+}
diff --git a/check_n_double_exist.py b/check_n_double_exist.py
new file mode 100644
index 0000000..cee0174
--- /dev/null
+++ b/check_n_double_exist.py
@@ -0,0 +1,26 @@
+# 1346. Check If N and Its Double Exist
+
+"""
+given an array 'arr' of integers, check if there exists two indices 'i' and
+'j' such that 'i != j', '0 <= i, j < arr.length', and 'arr[i] == 2 * arr[j]'
+"""
+
+
+class Solution(object):
+ def checkIfExist(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: bool
+ """
+ seen = set()
+ for i in arr:
+ if 2 * i in seen or i / 2 in seen:
+ return True
+ seen.add(i)
+ return False
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.checkIfExist(arr=[10, 2, 5, 3]))
+ print(obj.checkIfExist(arr=[3, 1, 7, 11]))