From 428b305d53de59408e6e0a4382eb8878436d32ae Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:04:19 +0530 Subject: [PATCH] Create 1346. Check If N and Its Double Exist --- 1346. Check If N and Its Double Exist | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1346. Check If N and Its Double Exist diff --git a/1346. Check If N and Its Double Exist b/1346. Check If N and Its Double Exist new file mode 100644 index 0000000..688fa35 --- /dev/null +++ b/1346. Check If N and Its Double Exist @@ -0,0 +1,18 @@ +class Solution { +public: + bool checkIfExist(vector& arr) { + int n = arr.size(); + bool cnt[2001] = {false}; // Boolean array to track whether a number has been seen + + for(int i = 0 ; i < n ; i++) { + int x = arr[i] * 2 + 1000; + if(x >= 0 && x <= 2000 && cnt[x]) //Check if 2 * arr[i] exists and not exceed the range + return true; + x = arr[i] / 2 + 1000; + if(arr[i] % 2 == 0 && cnt[x]) //ensure arr[i] is even and check if arr[i] / 2 exists + return true; + cnt[arr[i] + 1000] = 1; + } + return false; + } +};