Skip to content

Commit 3ed19a4

Browse files
authored
Merge branch 'master' into add-longest-substring
2 parents dbfa9d3 + 135fb08 commit 3ed19a4

File tree

8 files changed

+335
-1
lines changed

8 files changed

+335
-1
lines changed

DIRECTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
322322
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
323323
* [DigitSeparation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java)
324+
* [EgyptianFraction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/EgyptianFraction.java)
324325
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
325326
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
326327
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
@@ -351,6 +352,7 @@
351352
* [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java)
352353
* [BinomialCoefficient](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinomialCoefficient.java)
353354
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Ceil.java)
355+
* [ChineseRemainderTheorem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ChineseRemainderTheorem.java)
354356
* [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java)
355357
* [CollatzConjecture](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CollatzConjecture.java)
356358
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Combinations.java)
@@ -568,6 +570,8 @@
568570
* [TernarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/TernarySearch.java)
569571
* [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
570572
* [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
573+
* slidingwindow
574+
* [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
571575
* sorts
572576
* [AdaptiveMergeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java)
573577
* [BeadSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BeadSort.java)
@@ -826,6 +830,7 @@
826830
* [HashMapCuckooHashingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/HashMapCuckooHashingTest.java)
827831
* heaps
828832
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
833+
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
829834
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
830835
* lists
831836
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
@@ -942,6 +947,7 @@
942947
* [BinaryAdditionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java)
943948
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
944949
* [DigitSeparationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java)
950+
* [EgyptianFractionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/EgyptianFractionTest.java)
945951
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
946952
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
947953
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
@@ -969,6 +975,7 @@
969975
* [BinaryPowTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinaryPowTest.java)
970976
* [BinomialCoefficientTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinomialCoefficientTest.java)
971977
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
978+
* [ChineseRemainderTheoremTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ChineseRemainderTheoremTest.java)
972979
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
973980
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
974981
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
@@ -1155,6 +1162,8 @@
11551162
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
11561163
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
11571164
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
1165+
* slidingwindow
1166+
* [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
11581167
* sorts
11591168
* [AdaptiveMergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java)
11601169
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)

src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public class GenericHeap<T extends Comparable<T>> {
99
HashMap<T, Integer> map = new HashMap<>();
1010

1111
public void add(T item) {
12+
if (item == null) {
13+
throw new IllegalArgumentException("Cannot insert null into the heap.");
14+
}
15+
1216
this.data.add(item);
1317
map.put(item, this.data.size() - 1); //
1418
upHeapify(this.data.size() - 1);

src/main/java/com/thealgorithms/datastructures/lists/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Linked List
22
### Description
33

4-
LinkedList is a data structure in which data is stored in a linear manner. It usually contains a data field and a link to the memory location of the next mode.
4+
LinkedList is a data structure in which data is stored in a linear manner. It usually contains a data field and a link to the memory location of the next node.
55

66
### Structure
77

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Class to represent a fraction as a sum of unique unit fractions.
8+
* Example:
9+
* 2/3 = 1/2 + 1/6
10+
* 3/10 = 1/4 + 1/20
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class EgyptianFraction {
15+
private EgyptianFraction() {
16+
}
17+
18+
/**
19+
* Calculates the Egyptian Fraction representation of a given fraction.
20+
*
21+
* @param numerator the numerator of the fraction
22+
* @param denominator the denominator of the fraction
23+
* @return List of unit fractions represented as strings "1/x"
24+
*/
25+
public static List<String> getEgyptianFraction(int numerator, int denominator) {
26+
List<String> result = new ArrayList<>();
27+
while (numerator != 0) {
28+
int x = (int) Math.ceil((double) denominator / numerator);
29+
result.add("1/" + x);
30+
numerator = numerator * x - denominator;
31+
denominator = denominator * x;
32+
}
33+
return result;
34+
}
35+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @brief Implementation of the Chinese Remainder Theorem (CRT) algorithm
7+
* @details
8+
* The Chinese Remainder Theorem (CRT) is used to solve systems of
9+
* simultaneous congruences. Given several pairwise coprime moduli
10+
* and corresponding remainders, the algorithm finds the smallest
11+
* positive solution.
12+
*/
13+
public final class ChineseRemainderTheorem {
14+
private ChineseRemainderTheorem() {
15+
}
16+
17+
/**
18+
* @brief Solves the Chinese Remainder Theorem problem.
19+
* @param remainders The list of remainders.
20+
* @param moduli The list of pairwise coprime moduli.
21+
* @return The smallest positive solution that satisfies all the given congruences.
22+
*/
23+
public static int solveCRT(List<Integer> remainders, List<Integer> moduli) {
24+
int product = 1;
25+
int result = 0;
26+
27+
// Calculate the product of all moduli
28+
for (int mod : moduli) {
29+
product *= mod;
30+
}
31+
32+
// Apply the formula for each congruence
33+
for (int i = 0; i < moduli.size(); i++) {
34+
int partialProduct = product / moduli.get(i);
35+
int inverse = modInverse(partialProduct, moduli.get(i));
36+
result += remainders.get(i) * partialProduct * inverse;
37+
}
38+
39+
// Adjust result to be the smallest positive solution
40+
result = result % product;
41+
if (result < 0) {
42+
result += product;
43+
}
44+
45+
return result;
46+
}
47+
48+
/**
49+
* @brief Computes the modular inverse of a number with respect to a modulus using
50+
* the Extended Euclidean Algorithm.
51+
* @param a The number for which to find the inverse.
52+
* @param m The modulus.
53+
* @return The modular inverse of a modulo m.
54+
*/
55+
private static int modInverse(int a, int m) {
56+
int m0 = m;
57+
int x0 = 0;
58+
int x1 = 1;
59+
60+
if (m == 1) {
61+
return 0;
62+
}
63+
64+
while (a > 1) {
65+
int q = a / m;
66+
int t = m;
67+
68+
// m is remainder now, process same as Euclid's algorithm
69+
m = a % m;
70+
a = t;
71+
t = x0;
72+
73+
x0 = x1 - q * x0;
74+
x1 = t;
75+
}
76+
77+
// Make x1 positive
78+
if (x1 < 0) {
79+
x1 += m0;
80+
}
81+
82+
return x1;
83+
}
84+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class GenericHeapTest {
12+
13+
private GenericHeap<Integer> heap;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
heap = new GenericHeap<>();
18+
}
19+
20+
@Test
21+
public void testGenericHeapAddAndGet() {
22+
heap.add(19);
23+
heap.add(36);
24+
heap.add(100);
25+
heap.add(-17);
26+
heap.add(3);
27+
28+
// Check that the largest element (100) is at the top of the heap
29+
assertEquals(100, heap.get());
30+
}
31+
32+
@Test
33+
public void testGenericHeapRemove() {
34+
heap.add(19);
35+
heap.add(36);
36+
heap.add(100);
37+
heap.add(-17);
38+
heap.add(3);
39+
40+
// Verify that the largest element is removed correctly
41+
assertEquals(100, heap.remove());
42+
43+
// The new element at the top should be 36
44+
assertEquals(36, heap.get());
45+
46+
// Check that the size is correct after removal
47+
assertEquals(4, heap.size());
48+
}
49+
50+
@Test
51+
public void testGenericHeapSize() {
52+
assertTrue(heap.isEmpty());
53+
54+
heap.add(10);
55+
heap.add(20);
56+
57+
// Check that the size is correct
58+
assertEquals(2, heap.size());
59+
60+
heap.remove();
61+
62+
// After removal, the size should be 1
63+
assertEquals(1, heap.size());
64+
}
65+
66+
@Test
67+
public void testGenericHeapIsEmpty() {
68+
// Verify that the heap is initially empty
69+
assertTrue(heap.isEmpty());
70+
71+
heap.add(15);
72+
73+
// Now the heap should not be empty
74+
assertFalse(heap.isEmpty());
75+
76+
heap.remove();
77+
78+
// After removing the one element, it should be empty again
79+
assertTrue(heap.isEmpty());
80+
}
81+
82+
@Test
83+
public void testGenericHeapUpdatePriority() {
84+
heap.add(19);
85+
heap.add(36);
86+
heap.add(100);
87+
heap.add(-17);
88+
heap.add(3);
89+
90+
// Verify that the largest element initially is 100
91+
assertEquals(100, heap.get());
92+
93+
heap.remove();
94+
95+
// Simulates a change in priority by increasing the value of 100 to 44
96+
heap.add(44);
97+
98+
// Now, the new high should be 25
99+
assertEquals(44, heap.get());
100+
}
101+
102+
@Test
103+
public void testGenericHeapRemoveUntilEmpty() {
104+
heap.add(5);
105+
heap.add(3);
106+
heap.add(4);
107+
heap.add(1);
108+
heap.add(2);
109+
110+
// Remove all items and check that they are removed in descending order
111+
assertEquals(5, heap.remove());
112+
assertEquals(4, heap.remove());
113+
assertEquals(3, heap.remove());
114+
assertEquals(2, heap.remove());
115+
assertEquals(1, heap.remove());
116+
117+
// Empty heap
118+
assertTrue(heap.isEmpty());
119+
}
120+
121+
@Test
122+
public void testGenericHeapAddNullItem() {
123+
// Check null item
124+
assertThrows(IllegalArgumentException.class, () -> { heap.add(null); });
125+
}
126+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class EgyptianFractionTest {
12+
13+
@ParameterizedTest
14+
@MethodSource("fractionProvider")
15+
public void testGetEgyptianFraction(int numerator, int denominator, List<String> expected) {
16+
assertEquals(expected, EgyptianFraction.getEgyptianFraction(numerator, denominator));
17+
}
18+
19+
private static Stream<Arguments> fractionProvider() {
20+
return Stream.of(Arguments.of(2, 3, List.of("1/2", "1/6")), Arguments.of(3, 10, List.of("1/4", "1/20")), Arguments.of(1, 3, List.of("1/3")), Arguments.of(1, 2, List.of("1/2")), Arguments.of(4, 13, List.of("1/4", "1/18", "1/468")));
21+
}
22+
}

0 commit comments

Comments
 (0)