Skip to content

Commit d80c6f0

Browse files
add 1346
1 parent 071b5f6 commit d80c6f0

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1346|[Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1346.java) | |Easy|Array|
1112
|1345|[Jump Game IV](https://leetcode.com/problems/jump-game-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1345.java) | |Hard|BFS|
1213
|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1344.java) | |Medium|Math|
1314
|1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium|Array|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1346. Check If N and Its Double Exist
5+
*
6+
* 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).
7+
*
8+
* More formally check if there exists two indices i and j such that :
9+
*
10+
* i != j
11+
* 0 <= i, j < arr.length
12+
* arr[i] == 2 * arr[j]
13+
*
14+
* Example 1:
15+
* Input: arr = [10,2,5,3]
16+
* Output: true
17+
* Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
18+
*
19+
* Example 2:
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+
* Example 3:
25+
* Input: arr = [3,1,7,11]
26+
* Output: false
27+
* Explanation: In this case does not exist N and M, such that N = 2 * M.
28+
*
29+
* Constraints:
30+
* 2 <= arr.length <= 500
31+
* -10^3 <= arr[i] <= 10^3
32+
* */
33+
public class _1346 {
34+
public static class Solution1 {
35+
public boolean checkIfExist(int[] arr) {
36+
for (int i = 0; i < arr.length; i++) {
37+
for (int j = 0; j < arr.length; j++) {
38+
if (i != j && (arr[i] * 2 == arr[j] || arr[i] == arr[j] * 2)) {
39+
return true;
40+
}
41+
}
42+
}
43+
return false;
44+
}
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1346;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1346Test {
10+
private static _1346.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1346.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{10, 2, 5, 3};
21+
assertEquals(true, solution1.checkIfExist(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{7, 1, 14, 11};
27+
assertEquals(true, solution1.checkIfExist(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{3, 1, 7, 11};
33+
assertEquals(true, solution1.checkIfExist(arr));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)