1346. Check If N and Its Double Exist LeetCode Solution

In this guide, you will get 1346. Check If N and Its Double Exist LeetCode Solution with the best time and space complexity. The solution to Check If N and Its Double Exist problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check If N and Its Double Exist solution in C++
  4. Check If N and Its Double Exist solution in Java
  5. Check If N and Its Double Exist solution in Python
  6. Additional Resources
1346. Check If N and Its Double Exist LeetCode Solution image

Problem Statement of Check If N and Its Double Exist

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1346. Check If N and Its Double Exist LeetCode Solution in C++

class Solution {
 public:
  bool checkIfExist(vector<int>& arr) {
    unordered_set<int> seen;

    for (const int a : arr) {
      if (seen.contains(a * 2) || a % 2 == 0 && seen.contains(a / 2))
        return true;
      seen.insert(a);
    }

    return false;
  }
};
/* code provided by PROGIEZ */

1346. Check If N and Its Double Exist LeetCode Solution in Java

class Solution {
  public boolean checkIfExist(int[] arr) {
    Set<Integer> seen = new HashSet<>();

    for (final int a : arr) {
      if (seen.contains(a * 2) || a % 2 == 0 && seen.contains(a / 2))
        return true;
      seen.add(a);
    }

    return false;
  }
}
// code provided by PROGIEZ

1346. Check If N and Its Double Exist LeetCode Solution in Python

class Solution:
  def checkIfExist(self, arr: list[int]) -> bool:
    seen = set()

    for a in arr:
      if a * 2 in seen or a % 2 == 0 and a // 2 in seen:
        return True
      seen.add(a)

    return False
# code by PROGIEZ

Additional Resources

See also  1493. Longest Subarray of 1's After Deleting One Element LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.