> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/06-05-2022-1346.-check-if-n-and-its-double-exist.md).

# 06/05/2022 1346. Check If N and Its Double Exist

Method1: brutal force. sort the array first. Then we scan the array to find if  arr\[j] == 2 \* arr\[i] || arr\[i] == 2 \* arr\[j]. Noticed here, we cannot use divide because for example, when we use 3 / 2, we get 1 instead of 1.5. Use multiplication can avoid this mistake.

Time O(n^2)

Space O(1)

```
// Some code
class Solution {
    public boolean checkIfExist(int[] arr) {
        Arrays.sort(arr);
        int left = 0;
        int right = arr.length - 1;
        for (int i = 0; i < arr.length; i++){
            for (int j = i+ 1; j < arr.length; j++){
                if (arr[j] == 2 * arr[i] || arr[i] == 2 * arr[j]) return true;
            }
        }
        return false;
    }
}
```

Method 2: Use HashSet to add each element. If the set contains n \* 2 or n / 2 if n % 2 == 0 return true.&#x20;

Time O(n)

Space O(n)

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