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/KadanesAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithm.java new file mode 100644 index 000000000000..7b30ac657164 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithm.java @@ -0,0 +1,16 @@ +package com.thealgorithms.dynamicprogramming; + +public class KadanesAlgorithm { + public static int maxSubArraySum(int[] nums) { + if (nums == null || nums.length == 0) { + throw new IllegalArgumentException("Input array cannot be null or empty"); + } + int maxSoFar = nums[0]; + int currentMax = nums[0]; + for (int i = 1; i < nums.length; i++) { + currentMax = Math.max(nums[i], currentMax + nums[i]); + maxSoFar = Math.max(maxSoFar, currentMax); + } + return maxSoFar; + } +} 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 topologicalSort(int vertices, List> adjacencyList) { + boolean[] visited = new boolean[vertices]; + Stack stack = new Stack<>(); + + for (int i = 0; i < vertices; i++) { + if (!visited[i]) { + dfs(i, visited, stack, adjacencyList); + } + } + + List result = new ArrayList<>(); + while (!stack.isEmpty()) { + result.add(stack.pop()); + } + return result; + } + + private static void dfs(int node, boolean[] visited, Stack stack, List> 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 primes = new ArrayList<>(); + for (int i = 2; i <= n; i++) { + if (isPrime[i]) { + primes.add(i); } } - return primes; - } - - /** - * @brief finds all of the prime numbers up to the given upper (inclusive) limit - * @param n upper (inclusive) limit - * @exception IllegalArgumentException n is non-positive - * @return the array of all primes up to the given number (inclusive) - */ - public static int[] findPrimesTill(int n) { - return extractPrimes(sievePrimesTill(n)); - } - private enum Type { - PRIME, - NOT_PRIME, + return primes.stream().mapToInt(Integer::intValue).toArray(); } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFour.java new file mode 100644 index 000000000000..fde287f5415c --- /dev/null +++ b/src/test/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/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java new file mode 100644 index 000000000000..b202a58ed591 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/PowerOfFourTest.java @@ -0,0 +1,32 @@ +package com.thealgorithms.bitmanipulation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link PowerOfFour}. + */ +public final class PowerOfFourTest { + + @Test + void testPowerOfFourTrueCases() { + Assertions.assertTrue(PowerOfFour.isPowerOfFour(1)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(4)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(16)); + Assertions.assertTrue(PowerOfFour.isPowerOfFour(64)); + } + + @Test + void testPowerOfFourFalseCases() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(0)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(2)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(8)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(12)); + } + + @Test + void testNegativeNumbers() { + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-4)); + Assertions.assertFalse(PowerOfFour.isPowerOfFour(-16)); + } +} diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/TestKadane.java b/src/test/java/com/thealgorithms/dynamicprogramming/TestKadane.java new file mode 100644 index 000000000000..0c4d5a6b4844 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/TestKadane.java @@ -0,0 +1,12 @@ +package com.thealgorithms.dynamicprogramming; + +public class TestKadane { + public static void main(String[] args) { + int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + System.out.println(maxSubArraySum(arr)); // Expected: 6 + } + + private static int maxSubArraySum(int[] nums) { + return KadanesAlgorithm.maxSubArraySum(nums); + } +} diff --git a/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java b/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java new file mode 100644 index 000000000000..9c0c523971be --- /dev/null +++ b/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java @@ -0,0 +1,44 @@ +package com.thealgorithms.graph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link TopologicalSortDFS}. + */ +public final class TopologicalSortDFSTest { + + @Test + void testSimpleGraph() { + int vertices = 6; + 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 result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList); + + // A valid topological order is one of the possible ones + List expected = Arrays.asList(5, 4, 2, 3, 1, 0); + Assertions.assertTrue(result.containsAll(expected) && expected.containsAll(result)); + } + + @Test + void testEmptyGraph() { + int vertices = 0; + List> adjacencyList = new ArrayList<>(); + List result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList); + Assertions.assertTrue(result.isEmpty()); + } +} diff --git a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java index ebbd5df712fc..8d332f04c512 100644 --- a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java +++ b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java @@ -1,46 +1,23 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class SieveOfEratosthenesTest { - @Test - public void testfFindPrimesTill1() { - assertArrayEquals(new int[] {}, SieveOfEratosthenes.findPrimesTill(1)); - } - - @Test - public void testfFindPrimesTill2() { - assertArrayEquals(new int[] {2}, SieveOfEratosthenes.findPrimesTill(2)); - } - - @Test - public void testfFindPrimesTill4() { - var primesTill4 = new int[] {2, 3}; - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(3)); - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(4)); - } - - @Test - public void testfFindPrimesTill40() { - var primesTill40 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(37)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(38)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(39)); - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(40)); - } +/** + * Unit tests for {@link SieveOfEratosthenes}. + */ +public final class SieveOfEratosthenesTest { @Test - public void testfFindPrimesTill240() { - var primesTill240 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239}; - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(239)); - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(240)); + void testPrimesUpTo30() { + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; + Assertions.assertArrayEquals(expected, SieveOfEratosthenes.sieve(30)); } @Test - public void testFindPrimesTillThrowsExceptionForNonPositiveInput() { - assertThrows(IllegalArgumentException.class, () -> SieveOfEratosthenes.findPrimesTill(0)); + void testLessThanTwo() { + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(1)); + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(0)); + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(-5)); } }