Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 1346. Check If N and Its Double Exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool checkIfExist(vector<int>& 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;
}
};
Loading