Check If N and Its Double Exist
This solution creates a set to keep track of the numbers encountered so far, then for each number, it checks if its double or its half (if the number is even) is present in the set.
Python solution
|
|
This algorithm works because it considers each number in arr
in order, and for each number, it checks if its double or half is in the set of previously seen numbers. Therefore, if there are any two indices i
and j
such that arr[i] == 2 * arr[j]
, this pair will be detected either when considering index i
(if i > j
) or when considering index j
(if j > i
). If no such pair exists, we will iterate through the whole array and return False.