Skip to content

Commit 21c145c

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

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Good morning! Here's your coding interview problem for today.
2+
3+
This problem was asked by Netflix.
4+
5+
Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet `(a,
6+
b, c)` is defined by the equation `a^2`+ `b^2`= `c^2`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.devstromo.day1110;
2+
3+
public class Problem {
4+
public boolean solve(int[] nums) {
5+
for (int i = 0; i < nums.length - 2; i++) {
6+
if (nums[i] == 0) continue;
7+
for (int j = i + 1; j < nums.length - 1; j++) {
8+
if (nums[j] == 0) continue;
9+
for (int k = j + 1; k < nums.length; k++) {
10+
if (nums[k] == 0) continue;
11+
if (nums[i] * nums[i] + nums[j] * nums[j] == nums[k] * nums[k]) {
12+
return true;
13+
}
14+
if (nums[i] * nums[i] + nums[k] * nums[k] == nums[j] * nums[j]) {
15+
return true;
16+
}
17+
if (nums[k] * nums[k] + nums[j] * nums[j] == nums[i] * nums[i]) {
18+
return true;
19+
}
20+
}
21+
}
22+
}
23+
return false;
24+
}
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.devstromo.day1110;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ProblemUnitTest {
8+
private final Problem problem = new Problem();
9+
10+
@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() {
22+
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() {
37+
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() {
52+
assertTrue(problem.solve(new int[]{-3, 4, 5}));
53+
assertTrue(problem.solve(new int[]{3, -4, 5}));
54+
assertTrue(problem.solve(new int[]{3, 4, -5}));
55+
}
56+
57+
@Test
58+
void testLargeTriplet() {
59+
assertTrue(problem.solve(new int[]{9, 40, 41}));
60+
}
61+
}

0 commit comments

Comments
 (0)