# 1346. Check If N and Its Double Exist

Given an array `arr` of integers, check if there exist two indices `i` and `j` such that :

• `i != j`
• `0 <= i, j < arr.length`
• `arr[i] == 2 * arr[j]`

Example 1:

Input: arr = [10,2,5,3]
Output: true
Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]

Example 2:

Input: arr = [3,1,7,11]
Output: false
Explanation: There is no i and j that satisfy the conditions.

Constraints:

• `2 <= arr.length <= 500`
• `-10^3 <= arr[i] <= 10^3`

``````class Solution {
public:
bool checkIfExist(vector<int>& arr) {
unordered_map<int, int> m;
for (int i = 0; i < arr.size(); ++i) {
m[arr[i]] = i;
}
for (int i = 0; i < arr.size(); ++i) {
int t = arr[i] * 2;
if (m.count(t) && i != m[t]) return true;
}
return false;
}
};
``````

``````class Solution {
public:
bool checkIfExist(vector<int>& arr) {
unordered_set<int> st;
for (int num : arr) {
if (st.count(num * 2) || (num % 2 == 0 && st.count(num / 2))) return true;
st.insert(num);
}
return false;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1346

Two Sum

Keep Multiplying Found Values by Two

https://leetcode.com/problems/check-if-n-and-its-double-exist

https://leetcode.com/problems/check-if-n-and-its-double-exist/solutions/503441/java-python-3-hashset-w-analysis/

https://leetcode.com/problems/check-if-n-and-its-double-exist/solutions/504918/java-easy-5-line-solution-with-explanation/

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation