From c65cfd0f9bc7ee04b3c9c5c52546bab8ad0b54ee Mon Sep 17 00:00:00 2001 From: Nihal Singh Date: Wed, 29 Oct 2025 19:33:46 +0530 Subject: [PATCH 1/2] feat(slidingwindow): add SmallestSubarrayWithSum algorithm --- .../SmallestSubarrayWithSum.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java new file mode 100644 index 000000000000..9c544aed2819 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java @@ -0,0 +1,54 @@ +package com.thealgorithms.slidingwindow; + +/** + * The Sliding Window algorithm is used to find the length of the smallest + * contiguous subarray whose sum is greater than or equal to a given target. + * + *

Time Complexity: O(n) + *
Space Complexity: O(1) + * + *

This class provides a static method to find the minimum length subarray + * that satisfies the given sum condition. + * + *

Example: + *

+ * int[] nums = {2, 3, 1, 2, 4, 3};
+ * int target = 7;
+ * int result = SmallestSubarrayWithSum.findMinSubArrayLen(target, nums);
+ * System.out.println(result); // Output: 2 (subarray [4,3])
+ * 
+ * + * @author Nihal + */ +public final class SmallestSubarrayWithSum { + + // Prevent instantiation + private SmallestSubarrayWithSum() { + } + + /** + * Finds the minimal length of a contiguous subarray of which the sum ≥ target. + * + * @param target the target sum + * @param nums the array of positive integers + * @return the length of the smallest subarray with sum ≥ target, + * or 0 if no such subarray exists + */ + public static int findMinSubArrayLen(int target, int[] nums) { + int windowStart = 0; + int windowSum = 0; + int minLength = Integer.MAX_VALUE; + + for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) { + windowSum += nums[windowEnd]; + + while (windowSum >= target) { + minLength = Math.min(minLength, windowEnd - windowStart + 1); + windowSum -= nums[windowStart]; + windowStart++; + } + } + + return (minLength == Integer.MAX_VALUE) ? 0 : minLength; + } +} From 2d7a451364b5f0fbdd2b4ca66d51b14f6c6fe967 Mon Sep 17 00:00:00 2001 From: Nihal Singh Date: Wed, 29 Oct 2025 21:40:35 +0530 Subject: [PATCH 2/2] feat(slidingwindow): add SmallestSubarrayWithSum algorithm --- .../SmallestSubarrayWithSum.java | 56 ++++--------------- .../SmallestSubarrayWithSumTest.java | 50 +++++++++++++++++ 2 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 src/test/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSumTest.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java index 9c544aed2819..e6ecbca17f5e 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java +++ b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java @@ -1,54 +1,18 @@ package com.thealgorithms.slidingwindow; -/** - * The Sliding Window algorithm is used to find the length of the smallest - * contiguous subarray whose sum is greater than or equal to a given target. - * - *

Time Complexity: O(n) - *
Space Complexity: O(1) - * - *

This class provides a static method to find the minimum length subarray - * that satisfies the given sum condition. - * - *

Example: - *

- * int[] nums = {2, 3, 1, 2, 4, 3};
- * int target = 7;
- * int result = SmallestSubarrayWithSum.findMinSubArrayLen(target, nums);
- * System.out.println(result); // Output: 2 (subarray [4,3])
- * 
- * - * @author Nihal - */ -public final class SmallestSubarrayWithSum { +public class SmallestSubarrayWithSum { - // Prevent instantiation - private SmallestSubarrayWithSum() { - } - - /** - * Finds the minimal length of a contiguous subarray of which the sum ≥ target. - * - * @param target the target sum - * @param nums the array of positive integers - * @return the length of the smallest subarray with sum ≥ target, - * or 0 if no such subarray exists - */ - public static int findMinSubArrayLen(int target, int[] nums) { - int windowStart = 0; - int windowSum = 0; - int minLength = Integer.MAX_VALUE; + public static int smallestSubarrayLen(int[] arr, int target) { + if (arr == null || arr.length == 0) return 0; + int left = 0, sum = 0, minLen = Integer.MAX_VALUE; - for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) { - windowSum += nums[windowEnd]; - - while (windowSum >= target) { - minLength = Math.min(minLength, windowEnd - windowStart + 1); - windowSum -= nums[windowStart]; - windowStart++; + for (int right = 0; right < arr.length; right++) { + sum += arr[right]; + while (sum >= target) { + minLen = Math.min(minLen, right - left + 1); + sum -= arr[left++]; } } - - return (minLength == Integer.MAX_VALUE) ? 0 : minLength; + return minLen == Integer.MAX_VALUE ? 0 : minLen; } } diff --git a/src/test/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSumTest.java b/src/test/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSumTest.java new file mode 100644 index 000000000000..01c2427455de --- /dev/null +++ b/src/test/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSumTest.java @@ -0,0 +1,50 @@ +package com.thealgorithms.slidingwindow; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for SmallestSubarrayWithSum algorithm + */ +public class SmallestSubarrayWithSumTest { + + @Test + public void testBasicCase() { + int[] arr = {2, 3, 1, 2, 4, 3}; + int target = 7; + int expected = 2; // subarray [4,3] + assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target)); + } + + @Test + public void testNoValidSubarray() { + int[] arr = {1, 1, 1, 1}; + int target = 10; + int expected = 0; // no subarray reaches target + assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target)); + } + + @Test + public void testSingleElement() { + int[] arr = {5}; + int target = 5; + int expected = 1; + assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target)); + } + + @Test + public void testAllElementsSame() { + int[] arr = {2, 2, 2, 2, 2}; + int target = 6; + int expected = 3; + assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target)); + } + + @Test + public void testLargeInput() { + int[] arr = {1, 2, 3, 4, 5, 6}; + int target = 11; + int expected = 2; // subarray [5,6] + assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target)); + } +}