File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
solution/1346. Check If N and Its Double Exist Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Check If N and Its Double Exist
2
+
3
+ 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).
4
+
5
+ More formally check if there exists two indices i and j such that :
6
+ * i != j
7
+ * 0 <= i, j < arr.length
8
+ * arr[ i] == 2 * arr[ j]
9
+
10
+ ## Example 1:
11
+ ```
12
+ Input: arr = [10,2,5,3]
13
+ Output: true
14
+ Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
15
+
16
+ ```
17
+
18
+ ## Example 2:
19
+ ```
20
+ Input: arr = [7,1,14,11]
21
+ Output: true
22
+ Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
23
+
24
+ ```
25
+
26
+ ## Example 3:
27
+ ```
28
+ Input: arr = [3,1,7,11]
29
+ Output: false
30
+ Explanation: In this case does not exist N and M, such that N = 2 * M.
31
+
32
+ ```
33
+
34
+
35
+ ## Constraints:
36
+ * 2 <= arr.length <= 500
37
+ * -10^3 <= arr[ i] <= 10^3
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public boolean checkIfExist (int [] arr ) {
5
+ Map <Integer , Integer > map = new HashMap <>();
6
+ for (int i = 0 ; i < arr .length ; i ++) {
7
+ map .put (arr [i ], i );
8
+ }
9
+ for (int i = 0 ; i < arr .length ; i ++) {
10
+ if (map .containsKey (arr [i ] * 2 ) && i != map .get (arr [i ] * 2 ))
11
+ return true ;
12
+ }
13
+ return false ;
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments