Skip to content

Commit 3e0d6b9

Browse files
Merge branch 'master' into fix-7458-unify-dijkstra-priorityqueue
2 parents f4b67de + 631eae3 commit 3e0d6b9

6 files changed

Lines changed: 96 additions & 3 deletions

File tree

src/main/java/com/thealgorithms/tree/HeavyLightDecomposition.java renamed to src/main/java/com/thealgorithms/datastructures/trees/HeavyLightDecomposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

33
import java.util.ArrayList;
44
import java.util.List;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* The Padovan Sequence is a sequence of integers defined by the recurrence relation:
5+
* P(n) = P(n-2) + P(n-3) with initial values P(0) = P(1) = P(2) = 1.
6+
* Example: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37...
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Padovan_sequence">
9+
* Wikipedia: Padovan Sequence</a>
10+
* @author Vraj Prajapati (@Rosander0)
11+
*/
12+
public final class PadovanSequence {
13+
14+
private PadovanSequence() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Calculates the nth term of the Padovan Sequence.
20+
*
21+
* @param n the index of the sequence (must be non-negative)
22+
* @return the nth term of the Padovan Sequence
23+
*/
24+
public static long padovan(final int n) {
25+
if (n < 0) {
26+
throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
27+
}
28+
if (n <= 2) {
29+
return 1;
30+
}
31+
long a = 1;
32+
long b = 1;
33+
long c = 1;
34+
long result = 0;
35+
for (int i = 3; i <= n; i++) {
36+
result = a + b;
37+
a = b;
38+
b = c;
39+
c = result;
40+
}
41+
return result;
42+
}
43+
}

src/main/java/com/thealgorithms/searches/InterpolationSearch.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public int find(int[] array, int key) {
3636
// Since array is sorted, an element present
3737
// in array must be in range defined by corner
3838
while (start <= end && key >= array[start] && key <= array[end]) {
39+
if (array[start] == array[end]) {
40+
return start;
41+
}
3942
// Probing the position with keeping
4043
// uniform distribution in mind.
41-
int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
44+
int pos = start + (int) (((long) (end - start) * (key - array[start])) / ((long) array[end] - array[start]));
4245

4346
// Condition of target found
4447
if (array[pos] == key) {

src/test/java/com/thealgorithms/tree/HeavyLightDecompositionTest.java renamed to src/test/java/com/thealgorithms/datastructures/trees/HeavyLightDecompositionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* @author Vraj Prajapati (@Rosander0)
10+
*/
11+
public class PadovanSequenceTest {
12+
13+
@Test
14+
public void testBaseCase() {
15+
assertEquals(1, PadovanSequence.padovan(0));
16+
assertEquals(1, PadovanSequence.padovan(1));
17+
assertEquals(1, PadovanSequence.padovan(2));
18+
}
19+
20+
@Test
21+
public void testKnownValues() {
22+
assertEquals(2, PadovanSequence.padovan(3));
23+
assertEquals(2, PadovanSequence.padovan(4));
24+
assertEquals(3, PadovanSequence.padovan(5));
25+
assertEquals(4, PadovanSequence.padovan(6));
26+
assertEquals(5, PadovanSequence.padovan(7));
27+
assertEquals(7, PadovanSequence.padovan(8));
28+
assertEquals(9, PadovanSequence.padovan(9));
29+
assertEquals(12, PadovanSequence.padovan(10));
30+
}
31+
32+
@Test
33+
public void testInvalidInput() {
34+
assertThrows(IllegalArgumentException.class, () -> PadovanSequence.padovan(-1));
35+
}
36+
}

src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,15 @@ void testInterpolationSearchLargeNonUniformArray() {
8787
int key = 21; // Present in the array
8888
assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6.");
8989
}
90+
91+
/**
92+
* Test for interpolation search with specific sorted arrays that previously caused division by zero.
93+
*/
94+
@Test
95+
void testInterpolationSearchDivisionByZeroEdgeCases() {
96+
InterpolationSearch interpolationSearch = new InterpolationSearch();
97+
assertEquals(3, interpolationSearch.find(new int[] {0, 0, 0, 2}, 2));
98+
assertEquals(0, interpolationSearch.find(new int[] {2, 2, 2, 2}, 2));
99+
assertEquals(3, interpolationSearch.find(new int[] {0, 1, 2, 4}, 4));
100+
}
90101
}

0 commit comments

Comments
 (0)