Skip to content

Commit d0e77eb

Browse files
committed
Daily Coding Problem | Day 1110 | Pythagorean triplet
1 parent 21c145c commit d0e77eb

2 files changed

Lines changed: 63 additions & 41 deletions

File tree

daily-coding-problem/src/main/java/com/devstromo/day1110/Problem.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.devstromo.day1110;
22

3+
import java.util.Arrays;
4+
35
public class Problem {
46
public boolean solve(int[] nums) {
57
for (int i = 0; i < nums.length - 2; i++) {
@@ -22,4 +24,40 @@ public boolean solve(int[] nums) {
2224
}
2325
return false;
2426
}
27+
28+
public boolean solveTwoPointers(int[] nums) {
29+
int n = nums.length;
30+
31+
boolean allZero = Arrays.stream(nums).allMatch(x -> x == 0);
32+
if (allZero) return false;
33+
34+
int[] squared = Arrays.stream(nums)
35+
.map(x -> x * x)
36+
.toArray();
37+
38+
Arrays.sort(squared);
39+
40+
for (int i = n - 1; i >= 2; i--) {
41+
if (squared[i] == 0) continue;
42+
int left = 0;
43+
int right = i - 1;
44+
45+
while (left < right) {
46+
int sum = squared[left] + squared[right];
47+
48+
if (sum == squared[i]) {
49+
if (squared[left] != 0 || squared[right] != 0) return true;
50+
left++;
51+
right--;
52+
} else if (sum < squared[i]) {
53+
left++;
54+
} else {
55+
right--;
56+
}
57+
}
58+
}
59+
60+
return false;
61+
}
62+
2563
}
Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.devstromo.day1110;
22

3+
import org.junit.jupiter.api.DisplayName;
34
import org.junit.jupiter.api.Test;
45

56
import static org.junit.jupiter.api.Assertions.*;
@@ -8,54 +9,37 @@ class ProblemUnitTest {
89
private final Problem problem = new Problem();
910

1011
@Test
11-
void testSimpleTriplet() {
12-
assertTrue(problem.solve(new int[]{3, 4, 5}));
13-
}
14-
15-
@Test
16-
void testMultipleTriplets() {
17-
assertTrue(problem.solve(new int[]{10, 4, 6, 8, 5}));
18-
}
19-
20-
@Test
21-
void testNoTriplet() {
12+
@DisplayName("Test Pythagorean triplet")
13+
void testPythagoreanTriplet() {
14+
assertFalse(problem.solve(new int[]{0, 0, 0}));
2215
assertFalse(problem.solve(new int[]{1, 2, 3}));
23-
}
24-
25-
@Test
26-
void testEmptyArray() {
27-
assertFalse(problem.solve(new int[]{}));
28-
}
29-
30-
@Test
31-
void testSingleElement() {
32-
assertFalse(problem.solve(new int[]{5}));
33-
}
34-
35-
@Test
36-
void testOnlyTwoElements() {
3716
assertFalse(problem.solve(new int[]{5, 12}));
38-
}
39-
40-
@Test
41-
void testWithDuplicates() {
42-
assertTrue(problem.solve(new int[]{5, 5, 3, 4}));
43-
}
44-
45-
@Test
46-
void testWithZeros() {
47-
assertFalse(problem.solve(new int[]{0, 0, 0}));
48-
}
49-
50-
@Test
51-
void testNegativeNumbers() {
17+
assertFalse(problem.solve(new int[]{5}));
18+
assertFalse(problem.solve(new int[]{}));
5219
assertTrue(problem.solve(new int[]{-3, 4, 5}));
20+
assertTrue(problem.solve(new int[]{10, 4, 6, 8, 5}));
5321
assertTrue(problem.solve(new int[]{3, -4, 5}));
5422
assertTrue(problem.solve(new int[]{3, 4, -5}));
23+
assertTrue(problem.solve(new int[]{3, 4, 5}));
24+
assertTrue(problem.solve(new int[]{5, 5, 3, 4}));
25+
assertTrue(problem.solve(new int[]{9, 40, 41}));
5526
}
5627

5728
@Test
58-
void testLargeTriplet() {
59-
assertTrue(problem.solve(new int[]{9, 40, 41}));
29+
@DisplayName("Test Pythagorean triplet with two pointers")
30+
void testPythagoreanTripletWithTwoPointers() {
31+
assertFalse(problem.solveTwoPointers(new int[]{0, 0, 0}));
32+
assertFalse(problem.solveTwoPointers(new int[]{1, 2, 3}));
33+
assertFalse(problem.solveTwoPointers(new int[]{5, 12}));
34+
assertFalse(problem.solveTwoPointers(new int[]{5}));
35+
assertFalse(problem.solveTwoPointers(new int[]{}));
36+
assertTrue(problem.solveTwoPointers(new int[]{-3, 4, 5}));
37+
assertTrue(problem.solveTwoPointers(new int[]{10, 4, 6, 8, 5}));
38+
assertTrue(problem.solveTwoPointers(new int[]{3, -4, 5}));
39+
assertTrue(problem.solveTwoPointers(new int[]{3, 4, -5}));
40+
assertTrue(problem.solveTwoPointers(new int[]{3, 4, 5}));
41+
assertTrue(problem.solveTwoPointers(new int[]{5, 5, 3, 4}));
42+
assertTrue(problem.solveTwoPointers(new int[]{9, 40, 41}));
6043
}
44+
6145
}

0 commit comments

Comments
 (0)