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; + } +};