Post

1468 Check If N And Its Double Exist

1468 Check If N And Its Double Exist

Check If N and Its Double Exist image

Given an array arr of integers, check if there exist two indices i and j such that :

1
2
3
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]

 

Example 1:

1
2
3
4
5
**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:

1
2
3
4
5
**Input:** arr = [3,1,7,11]
**Output:** false
**Explanation:** There is no i and j that satisfy the conditions.

 

Constraints:

1
2
2 <= arr.length <= 500
-103 <= arr[i] <= 103
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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

        for k in arr:
            
            if k in dic:
                return True
            
            #print(dic)
            if k % 2 == 0: 
                dic.add(int(k//2))
            dic.add(k*2)
        
        return False



            

        



This post is licensed under CC BY 4.0 by the author.