Check If N and Its Double Exist LeetCode Solution

Problem – Check If N and Its Double Exist LeetCode Solution

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]

Example 1:

Input: arr = [10,2,5,3]
Output: true
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.

Example 2:

Input: arr = [7,1,14,11]
Output: true
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.

Example 3:

Input: arr = [3,1,7,11]
Output: false
Explanation: In this case does not exist N and M, such that N = 2 * M.

Constraints:

  • 2 <= arr.length <= 500
  • -10^3 <= arr[i] <= 10^3

Check If N and Its Double Exist LeetCode Solution in Java

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

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

class Solution {
public:
    bool checkIfExist(vector<int>& arr) {
        unordered_set<int> set;
        for(int i=0;i<arr.size();i++){
            if(set.count(2*arr[i])>0 || ((arr[i]%2==0) && set.count(arr[i]/2)>0))
                return true;
            set.insert(arr[i]);
        }
        return false;
    }
};

Check If N and Its Double Exist LeetCode Solution in Python

class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        possible_M = set()
        possible_N = set()
        for a in arr:
            if a in possible_M: return True
            if a in possible_N: return True
            if a % 2 == 0: possible_M.add(a//2)
            possible_N.add(a*2)
        return False
Check If N and Its Double Exist LeetCode Solution Review:

In our experience, we suggest you solve this Check If N and Its Double Exist LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Check If N and Its Double Exist LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Check If N and Its Double Exist LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.