From 11cbdbf76dfe36196b87c912481416e30c586a82 Mon Sep 17 00:00:00 2001 From: Nihal Singh Date: Thu, 30 Oct 2025 01:33:02 +0530 Subject: [PATCH 1/2] feat(slidingwindow): add SmallestSubarrayWithSum and its tests --- .../SmallestSubarrayWithSum.java | 47 +++++++++++++++++ .../SmallestSubarrayWithSumTest.java | 50 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java 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 new file mode 100644 index 000000000000..d36307df5729 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java @@ -0,0 +1,47 @@ +package com.thealgorithms.slidingwindow; + +/** + * Finds the length of the smallest contiguous subarray + * whose sum is greater than or equal to a given target. + * + *

Example: + * arr = [2, 3, 1, 2, 4, 3], target = 7 + * Smallest subarray = [4, 3], length = 2 + * + *

Time complexity: O(n) + * Space complexity: O(1) + */ +public final class SmallestSubarrayWithSum { + + // Prevent instantiation + private SmallestSubarrayWithSum() {} + + /** + * Returns the minimal length of a contiguous subarray of which + * the sum ≥ target. If no such subarray exists, returns 0. + * + * @param arr the input array + * @param target the target sum + * @return the minimal subarray length, or 0 if not found + */ + public static int smallestSubarrayLen(int[] arr, int target) { + if (arr == null || arr.length == 0) { + return 0; + } + + int left = 0; + int sum = 0; + int minLen = Integer.MAX_VALUE; + + 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 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..592373e91675 --- /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 {@link SmallestSubarrayWithSum}. + */ +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; + 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)); + } +} From e671ff05b4f5d9683f8722c909d6232db483eea7 Mon Sep 17 00:00:00 2001 From: Nihal Singh Date: Thu, 30 Oct 2025 01:52:31 +0530 Subject: [PATCH 2/2] style: fix whitespace and formatting for SmallestSubarrayWithSum --- .../thealgorithms/slidingwindow/SmallestSubarrayWithSum.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java index d36307df5729..895f5c70d6cb 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java +++ b/src/main/java/com/thealgorithms/slidingwindow/SmallestSubarrayWithSum.java @@ -14,7 +14,8 @@ public final class SmallestSubarrayWithSum { // Prevent instantiation - private SmallestSubarrayWithSum() {} + private SmallestSubarrayWithSum() { + } /** * Returns the minimal length of a contiguous subarray of which @@ -44,4 +45,3 @@ public static int smallestSubarrayLen(int[] arr, int target) { return minLen == Integer.MAX_VALUE ? 0 : minLen; } } -