Leetcode - Check If N and Its Double Exist
https://leetcode.com/problems/check-if-n-and-its-double-exist/
Given an array arr
of integers, check if there exists two integers N
and M
such that N
is the double of M
( i.e. N = 2 * M
).
More formally check if there exists two indices i
and j
such that :
-
i != j
-
0 <= i, j < arr.length
-
arr[i] == 2 * arr[j]
Time complexity: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | func checkIfExist(arr [] int ) bool { hash := map[ int ] bool {} count0 := 0 for i := 0; i < len(arr); i++ { if arr[i] == 0 { count0++ } hash[arr[i]*2] = true } if count0 > 1 { return true } for i := 0; i < len(arr); i++ { if hash[arr[i]] && arr[i] != 0 { return true } } return false } |