diff --git a/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..f91f8412b3ce --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/PowerOfFour.java @@ -0,0 +1,26 @@ +package com.thealgorithms.bitmanipulation; + +/** + * This class provides a method to check if a given number is a power of four. + */ +public final class PowerOfFour { + + /** Private constructor to prevent instantiation. */ + private PowerOfFour() { + throw new AssertionError("Cannot instantiate utility class."); + } + + /** + * Checks whether the given integer is a power of four. + * + * @param n the number to check + * @return true if n is a power of four, false otherwise + */ + public static boolean isPowerOfFour(int n) { + if (n <= 0) { + return false; + } else { + return (n & (n - 1)) == 0 && (n & 0x55555555) != 0; + } + } +} diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java new file mode 100644 index 000000000000..063b31f3f44f --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java @@ -0,0 +1,24 @@ +package com.thealgorithms.dynamicprogramming; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class KadanesAlgorithmTest { + @Test + public void testPositiveNumbers() { + int[] arr = {1, 2, 3, 4}; + Assertions.assertEquals(10, KadanesAlgorithm.maxSubArraySum(arr)); + } + + @Test + public void testNegativeNumbers() { + int[] arr = {-1, -2, -3, -4}; + Assertions.assertEquals(-1, KadanesAlgorithm.maxSubArraySum(arr)); + } + + @Test + public void testMixedNumbers() { + int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + Assertions.assertEquals(6, KadanesAlgorithm.maxSubArraySum(arr)); // [4, -1, 2, 1] + } +} diff --git a/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java b/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java new file mode 100644 index 000000000000..965134c14b5b --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java @@ -0,0 +1,54 @@ +package com.thealgorithms.graph; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Implementation of Topological Sort using Depth-First Search (DFS). + * + *
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering
+ * of vertices such that for every directed edge u → v, vertex u comes before v
+ * in the ordering.
+ */
+public final class TopologicalSortDFS {
+
+ // Private constructor to prevent instantiation
+ private TopologicalSortDFS() {
+ throw new AssertionError("Cannot instantiate utility class");
+ }
+
+ /**
+ * Performs topological sorting on a directed acyclic graph.
+ *
+ * @param vertices the number of vertices in the graph
+ * @param adjacencyList the adjacency list representing the graph
+ * @return a list containing vertices in topologically sorted order
+ */
+ public static List> adjacencyList) {
+ boolean[] visited = new boolean[vertices];
+ Stack
> adjacencyList) {
+ visited[node] = true;
+ for (int neighbor : adjacencyList.get(node)) {
+ if (!visited[neighbor]) {
+ dfs(neighbor, visited, stack, adjacencyList);
+ }
+ }
+ stack.push(node);
+ }
+}
diff --git a/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java b/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java
index f22d22e8c6af..e9e5e2c34ef5 100644
--- a/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java
+++ b/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java
@@ -1,66 +1,50 @@
package com.thealgorithms.maths;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
/**
- * @brief utility class implementing Sieve of Eratosthenes
+ * Utility class that provides the Sieve of Eratosthenes algorithm.
*/
public final class SieveOfEratosthenes {
+
+ /** Private constructor to prevent instantiation. */
private SieveOfEratosthenes() {
+ throw new AssertionError("Cannot instantiate utility class.");
}
- private static void checkInput(int n) {
- if (n <= 0) {
- throw new IllegalArgumentException("n must be positive.");
+ /**
+ * Returns an array of all prime numbers less than or equal to {@code n}.
+ *
+ * @param n the upper bound (inclusive)
+ * @return array of primes <= n (empty if n < 2)
+ */
+ public static int[] sieve(final int n) {
+ if (n < 2) {
+ return new int[0];
}
- }
- private static Type[] sievePrimesTill(int n) {
- checkInput(n);
- Type[] isPrimeArray = new Type[n + 1];
- Arrays.fill(isPrimeArray, Type.PRIME);
- isPrimeArray[0] = Type.NOT_PRIME;
- isPrimeArray[1] = Type.NOT_PRIME;
+ boolean[] isPrime = new boolean[n + 1];
+ Arrays.fill(isPrime, true);
+ isPrime[0] = false;
+ isPrime[1] = false;
- double cap = Math.sqrt(n);
- for (int i = 2; i <= cap; i++) {
- if (isPrimeArray[i] == Type.PRIME) {
- for (int j = 2; i * j <= n; j++) {
- isPrimeArray[i * j] = Type.NOT_PRIME;
+ for (int p = 2; p * p <= n; p++) {
+ if (isPrime[p]) {
+ for (int multiple = p * p; multiple <= n; multiple += p) {
+ isPrime[multiple] = false;
}
}
}
- return isPrimeArray;
- }
-
- private static int countPrimes(Type[] isPrimeArray) {
- return (int) Arrays.stream(isPrimeArray).filter(element -> element == Type.PRIME).count();
- }
- private static int[] extractPrimes(Type[] isPrimeArray) {
- int numberOfPrimes = countPrimes(isPrimeArray);
- int[] primes = new int[numberOfPrimes];
- int primeIndex = 0;
- for (int curNumber = 0; curNumber < isPrimeArray.length; ++curNumber) {
- if (isPrimeArray[curNumber] == Type.PRIME) {
- primes[primeIndex++] = curNumber;
+ List
> adjacencyList = new ArrayList<>();
+ for (int i = 0; i < vertices; i++) {
+ adjacencyList.add(new ArrayList<>());
+ }
+
+ adjacencyList.get(5).add(2);
+ adjacencyList.get(5).add(0);
+ adjacencyList.get(4).add(0);
+ adjacencyList.get(4).add(1);
+ adjacencyList.get(2).add(3);
+ adjacencyList.get(3).add(1);
+
+ List
> adjacencyList = new ArrayList<>();
+ List