Skip to content

1346. Check If N and Its Double Exist #899

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a hash table (associative array) to track the elements we have already encountered while iterating through the array. The idea is to check for each element arr[i] if its double (i.e., 2 * arr[i]) or half (i.e., arr[i] / 2 if it's an even number) has already been encountered.

Here’s a step-by-step solution:

Plan:

  1. Iterate through the array.
  2. For each element arr[i], check if we have seen 2 * arr[i] or arr[i] / 2 (if arr[i] is even) in the hash table.
  3. If any condition is satisfied, return true.
  4. Otherwise, add arr[i] to the hash table and continue to the next element.
  5. If no match is found by the end, return false.

Let's implement this solution in PHP: 1346. Check If N and Its Doub…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Dec 1, 2024
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Dec 1, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 1, 2024
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants