Skip to content

Commit 338859c

Browse files
authored
Merge branch 'master' into master
2 parents e9af514 + 5a1f681 commit 338859c

File tree

12 files changed

+323
-54
lines changed

12 files changed

+323
-54
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@
982982
* [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
983983
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
984984
* [KaratsubaMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaratsubaMultiplicationTest.java)
985+
* [KrishnamurthyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KrishnamurthyNumberTest.java)
985986
* [LeastCommonMultipleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java)
986987
* [LeonardoNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java)
987988
* [LiouvilleLambdaFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java)

src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public static int cutRod(int[] price, int n) {
2222
if (price == null || price.length == 0) {
2323
throw new IllegalArgumentException("Price array cannot be null or empty.");
2424
}
25+
if (n < 0) {
26+
throw new IllegalArgumentException("Rod length cannot be negative.");
27+
}
28+
2529
// Create an array to store the maximum obtainable values for each rod length.
2630
int[] val = new int[n + 1];
2731
val[0] = 0;

src/main/java/com/thealgorithms/maths/GCD.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* This is Euclid's algorithm, used to find the greatest common
5-
* denominator Override function name gcd
4+
* This class provides methods to compute the Greatest Common Divisor (GCD) of two or more integers.
65
*
6+
* The Greatest Common Divisor (GCD) of two or more integers is the largest positive integer that divides each of the integers without leaving a remainder.
7+
*
8+
* The GCD can be computed using the Euclidean algorithm, which is based on the principle that the GCD of two numbers also divides their difference.
9+
*
10+
* For more information, refer to the
11+
* <a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor</a> Wikipedia page.
12+
*
13+
* <b>Example usage:</b>
14+
* <pre>
15+
* int result1 = GCD.gcd(48, 18);
16+
* System.out.println("GCD of 48 and 18: " + result1); // Output: 6
17+
*
18+
* int result2 = GCD.gcd(48, 18, 30);
19+
* System.out.println("GCD of 48, 18, and 30: " + result2); // Output: 6
20+
* </pre>
721
* @author Oskar Enmalm 3/10/17
822
*/
923
public final class GCD {
@@ -40,20 +54,12 @@ public static int gcd(int num1, int num2) {
4054
* @param numbers the input array
4155
* @return gcd of all of the numbers in the input array
4256
*/
43-
public static int gcd(int[] numbers) {
57+
public static int gcd(int... numbers) {
4458
int result = 0;
4559
for (final var number : numbers) {
4660
result = gcd(result, number);
4761
}
4862

4963
return result;
5064
}
51-
52-
public static void main(String[] args) {
53-
int[] myIntArray = {4, 16, 32};
54-
55-
// call gcd function (input array)
56-
System.out.println(gcd(myIntArray)); // => 4
57-
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d%n", gcd(40, 24), gcd(24, 40)); // => 8
58-
}
5965
}
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
package com.thealgorithms.maths;
22

3-
/* This is a program to check if a number is a Krishnamurthy number or not.
4-
A number is a Krishnamurthy number if the sum of the factorials of the digits of the number is equal
5-
to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
6-
also referred to as a Strong number.
3+
/**
4+
* Utility class for checking if a number is a Krishnamurthy number.
5+
*
6+
* A Krishnamurthy number (also known as a Strong number) is a number whose sum of the factorials of its digits is equal to the number itself.
7+
*
8+
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
9+
* <b>Example usage:</b>
10+
* <pre>
11+
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
12+
* System.out.println(isKrishnamurthy); // Output: true
13+
*
14+
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
15+
* System.out.println(isKrishnamurthy); // Output: false
16+
* </pre>
717
*/
8-
import java.io.BufferedReader;
9-
import java.io.IOException;
10-
import java.io.InputStreamReader;
11-
1218
public final class KrishnamurthyNumber {
19+
1320
private KrishnamurthyNumber() {
1421
}
1522

16-
// returns True if the number is a Krishnamurthy number and False if it is not.
17-
18-
public static boolean isKMurthy(int n) {
19-
// initialising the variable s that will store the sum of the factorials of the digits to 0
20-
int s = 0;
21-
// storing the number n in a temporary variable tmp
23+
/**
24+
* Checks if a number is a Krishnamurthy number.
25+
*
26+
* @param n The number to check
27+
* @return true if the number is a Krishnamurthy number, false otherwise
28+
*/
29+
public static boolean isKrishnamurthy(int n) {
2230
int tmp = n;
31+
int s = 0;
2332

24-
// Krishnamurthy numbers are positive
2533
if (n <= 0) {
2634
return false;
27-
} // checking if the number is a Krishnamurthy number
28-
else {
35+
} else {
2936
while (n != 0) {
3037
// initialising the variable fact that will store the factorials of the digits
3138
int fact = 1;
@@ -43,15 +50,4 @@ public static boolean isKMurthy(int n) {
4350
return tmp == s;
4451
}
4552
}
46-
47-
public static void main(String[] args) throws IOException {
48-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
49-
System.out.println("Enter a number to check if it is a Krishnamurthy number: ");
50-
int n = Integer.parseInt(br.readLine());
51-
if (isKMurthy(n)) {
52-
System.out.println(n + " is a Krishnamurthy number.");
53-
} else {
54-
System.out.println(n + " is NOT a Krishnamurthy number.");
55-
}
56-
}
5753
}

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,54 @@
33
import com.thealgorithms.datastructures.Node;
44
import java.util.ArrayDeque;
55
import java.util.ArrayList;
6+
import java.util.HashSet;
67
import java.util.List;
78
import java.util.Optional;
89
import java.util.Queue;
10+
import java.util.Set;
911

1012
/**
11-
* @author: caos321
12-
* @date: 31 October 2021 (Sunday)
13-
* @wiki: https://en.wikipedia.org/wiki/Breadth-first_search
13+
* Breadth-First Search implementation for tree/graph traversal.
14+
* @author caos321
15+
* @co-author @manishraj27
16+
* @see <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Breadth-first search</a>
1417
*/
1518
public class BreadthFirstSearch<T> {
16-
1719
private final List<T> visited = new ArrayList<>();
20+
private final Set<T> visitedSet = new HashSet<>();
1821

19-
public Optional<Node<T>> search(final Node<T> node, final T value) {
20-
if (node == null) {
22+
/**
23+
* Performs a breadth-first search to find a node with the given value.
24+
*
25+
* @param root The root node to start the search from
26+
* @param value The value to search for
27+
* @return Optional containing the found node, or empty if not found
28+
*/
29+
public Optional<Node<T>> search(final Node<T> root, final T value) {
30+
if (root == null) {
2131
return Optional.empty();
2232
}
23-
if (node.getValue().equals(value)) {
24-
// add root node to visited
25-
visited.add(value);
26-
return Optional.of(node);
27-
}
28-
visited.add(node.getValue());
2933

30-
Queue<Node<T>> queue = new ArrayDeque<>(node.getChildren());
34+
visited.add(root.getValue());
35+
visitedSet.add(root.getValue());
36+
37+
if (root.getValue() == value) {
38+
return Optional.of(root);
39+
}
3140

41+
Queue<Node<T>> queue = new ArrayDeque<>(root.getChildren());
3242
while (!queue.isEmpty()) {
3343
final Node<T> current = queue.poll();
34-
visited.add(current.getValue());
44+
T currentValue = current.getValue();
45+
46+
if (visitedSet.contains(currentValue)) {
47+
continue;
48+
}
49+
50+
visited.add(currentValue);
51+
visitedSet.add(currentValue);
3552

36-
if (current.getValue().equals(value)) {
53+
if (currentValue == value || (value != null && value.equals(currentValue))) {
3754
return Optional.of(current);
3855
}
3956

@@ -43,6 +60,11 @@ public Optional<Node<T>> search(final Node<T> node, final T value) {
4360
return Optional.empty();
4461
}
4562

63+
/**
64+
* Returns the list of nodes in the order they were visited.
65+
*
66+
* @return List containing the visited nodes
67+
*/
4668
public List<T> getVisited() {
4769
return visited;
4870
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class AdaptiveMergeSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length <= 1) {
7+
return array;
8+
}
9+
T[] aux = array.clone();
10+
sort(array, aux, 0, array.length - 1);
11+
return array;
12+
}
13+
14+
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
15+
if (low >= high) {
16+
return;
17+
}
18+
int mid = low + (high - low) / 2;
19+
sort(array, aux, low, mid);
20+
sort(array, aux, mid + 1, high);
21+
merge(array, aux, low, mid, high);
22+
}
23+
24+
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
25+
System.arraycopy(array, low, aux, low, high - low + 1);
26+
int i = low;
27+
int j = mid + 1;
28+
for (int k = low; k <= high; k++) {
29+
if (i > mid) {
30+
array[k] = aux[j++];
31+
} else if (j > high) {
32+
array[k] = aux[i++];
33+
} else if (aux[j].compareTo(aux[i]) < 0) {
34+
array[k] = aux[j++];
35+
} else {
36+
array[k] = aux[i++];
37+
}
38+
}
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class StalinSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length == 0) {
7+
return array;
8+
}
9+
int currentIndex = 0;
10+
for (int i = 1; i < array.length; i++) {
11+
if (array[i].compareTo(array[currentIndex]) >= 0) {
12+
currentIndex++;
13+
array[currentIndex] = array[i];
14+
}
15+
}
16+
// Create a result array with sorted elements
17+
T[] result = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), currentIndex + 1);
18+
System.arraycopy(array, 0, result, 0, currentIndex + 1);
19+
return result;
20+
}
21+
}

src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ void testCutRodEmptyPrices() {
9393
int length = 5;
9494
assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException.");
9595
}
96+
@Test
97+
void testCutRodNegativeLength() {
98+
int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for negative length
99+
int length = -1;
100+
assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "A negative rod length should throw an IllegalArgumentException.");
101+
}
96102
}

src/test/java/com/thealgorithms/maths/GCDTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ void test7() {
4040
Assertions.assertEquals(GCD.gcd(9, 6), 3);
4141
}
4242

43+
@Test
44+
void test8() {
45+
Assertions.assertEquals(GCD.gcd(48, 18, 30, 12), 6);
46+
}
47+
4348
@Test
4449
void testArrayGcd1() {
4550
Assertions.assertEquals(GCD.gcd(new int[] {9, 6}), 3);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the KrishnamurthyNumber class.
10+
*/
11+
public class KrishnamurthyNumberTest {
12+
13+
/**
14+
* Test the isKrishnamurthy method with a known Krishnamurthy number.
15+
*/
16+
@Test
17+
public void testIsKrishnamurthyTrue() {
18+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(145));
19+
}
20+
21+
/**
22+
* Test the isKrishnamurthy method with a number that is not a Krishnamurthy number.
23+
*/
24+
@Test
25+
public void testIsKrishnamurthyFalse() {
26+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(123));
27+
}
28+
29+
/**
30+
* Test the isKrishnamurthy method with zero.
31+
*/
32+
@Test
33+
public void testIsKrishnamurthyZero() {
34+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(0));
35+
}
36+
37+
/**
38+
* Test the isKrishnamurthy method with a negative number.
39+
*/
40+
@Test
41+
public void testIsKrishnamurthyNegative() {
42+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(-145));
43+
}
44+
45+
/**
46+
* Test the isKrishnamurthy method with a single-digit Krishnamurthy number.
47+
*/
48+
@Test
49+
public void testIsKrishnamurthySingleDigitTrue() {
50+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(1));
51+
assertTrue(KrishnamurthyNumber.isKrishnamurthy(2));
52+
}
53+
54+
/**
55+
* Test the isKrishnamurthy method with a single-digit number that is not a Krishnamurthy number.
56+
*/
57+
@Test
58+
public void testIsKrishnamurthySingleDigitFalse() {
59+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(3));
60+
assertFalse(KrishnamurthyNumber.isKrishnamurthy(4));
61+
}
62+
}

0 commit comments

Comments
 (0)