# 1346. Check If N and Its Double Exist

## Description

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
• -103 <= arr[i] <= 103

## Solutions

• class Solution {
public boolean checkIfExist(int[] arr) {
Map<Integer, Integer> m = new HashMap<>();
int n = arr.length;
for (int i = 0; i < n; ++i) {
m.put(arr[i], i);
}
for (int i = 0; i < n; ++i) {
if (m.containsKey(arr[i] << 1) && m.get(arr[i] << 1) != i) {
return true;
}
}
return false;
}
}

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

• class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
m = {v: i for i, v in enumerate(arr)}
return any(v << 1 in m and m[v << 1] != i for i, v in enumerate(arr))


• func checkIfExist(arr []int) bool {
m := make(map[int]int)
for i, v := range arr {
m[v] = i
}
for i, v := range arr {
if j, ok := m[v*2]; ok && j != i {
return true
}
}
return false
}

• function checkIfExist(arr: number[]): boolean {
const s = new Set();
for (const v of arr) {
if (s.has(v << 1) || s.has(v / 2)) {
return true;
}
}
return false;
}


• /**
* @param {number[]} arr
* @return {boolean}
*/
var checkIfExist = function (arr) {
const s = new Set();
for (const v of arr) {
if (s.has(v << 1) || s.has(v / 2)) {
return true;
}
}
return false;
};


• class Solution {
/**
* @param Integer[] $arr * @return Boolean */ function checkIfExist($arr) {
for ($i = 0;$i < count($arr);$i++) {
$hashtable[$arr[$i] * 2] =$i;
}
for ($i = 0;$i < count($arr);$i++) {
if (isset($hashtable[$arr[$i]]) &&$hashtable[$arr[$i]] != \$i) {
return true;
}
}
return false;
}
}


• use std::collections::HashMap;
impl Solution {
pub fn check_if_exist(arr: Vec<i32>) -> bool {
let mut map = HashMap::new();
for (i, v) in arr.iter().enumerate() {
map.insert(v, i);
}
for (i, v) in arr.iter().enumerate() {
if map.contains_key(&(v * 2)) && map[&(v * 2)] != i {
return true;
}
}
false
}
}