Skip to content

Commit 4c1730e

Browse files
authored
Create 1346. Check If N and Its Double Exist (#648)
2 parents 173402a + 428b305 commit 4c1730e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

1346. Check If N and Its Double Exist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool checkIfExist(vector<int>& arr) {
4+
int n = arr.size();
5+
bool cnt[2001] = {false}; // Boolean array to track whether a number has been seen
6+
7+
for(int i = 0 ; i < n ; i++) {
8+
int x = arr[i] * 2 + 1000;
9+
if(x >= 0 && x <= 2000 && cnt[x]) //Check if 2 * arr[i] exists and not exceed the range
10+
return true;
11+
x = arr[i] / 2 + 1000;
12+
if(arr[i] % 2 == 0 && cnt[x]) //ensure arr[i] is even and check if arr[i] / 2 exists
13+
return true;
14+
cnt[arr[i] + 1000] = 1;
15+
}
16+
return false;
17+
}
18+
};

0 commit comments

Comments
 (0)