LeetCode 1346: Check If N and Its Double Exist Solution

Master LeetCode problem 1346 (Check If N and Its Double Exist), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1346. Check If N and Its Double Exist

Problem Explanation

Explanation

To solve this problem, we can iterate through the array and for each element num, check if either num*2 or num/2 exists in a HashSet. If it does, then we have found the pair that satisfies the condition.

  • Time Complexity: O(n), where n is the number of elements in the array.
  • Space Complexity: O(n) for the HashSet.

Solution Code

class Solution {
    public boolean checkIfExist(int[] arr) {
        Set<Integer> set = new HashSet<>();
        
        for (int num : arr) {
            if (set.contains(num * 2) || (num % 2 == 0 && set.contains(num / 2))) {
                return true;
            }
            set.add(num);
        }
        
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1346 (Check If N and Its Double Exist)?

This page provides optimized solutions for LeetCode problem 1346 (Check If N and Its Double Exist) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1346 (Check If N and Its Double Exist)?

The time complexity for LeetCode 1346 (Check If N and Its Double Exist) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1346 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1346 in Java, C++, or Python.

Back to LeetCode Solutions