Skip to content

Commit 2d7a451

Browse files
committed
feat(slidingwindow): add SmallestSubarrayWithSum algorithm
1 parent c65cfd0 commit 2d7a451

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed
Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,18 @@
11
package com.thealgorithms.slidingwindow;
22

3-
/**
4-
* The Sliding Window algorithm is used to find the length of the smallest
5-
* contiguous subarray whose sum is greater than or equal to a given target.
6-
*
7-
* <p>Time Complexity: O(n)
8-
* <br>Space Complexity: O(1)
9-
*
10-
* <p>This class provides a static method to find the minimum length subarray
11-
* that satisfies the given sum condition.
12-
*
13-
* <p>Example:
14-
* <pre>
15-
* int[] nums = {2, 3, 1, 2, 4, 3};
16-
* int target = 7;
17-
* int result = SmallestSubarrayWithSum.findMinSubArrayLen(target, nums);
18-
* System.out.println(result); // Output: 2 (subarray [4,3])
19-
* </pre>
20-
*
21-
* @author Nihal
22-
*/
23-
public final class SmallestSubarrayWithSum {
3+
public class SmallestSubarrayWithSum {
244

25-
// Prevent instantiation
26-
private SmallestSubarrayWithSum() {
27-
}
28-
29-
/**
30-
* Finds the minimal length of a contiguous subarray of which the sum ≥ target.
31-
*
32-
* @param target the target sum
33-
* @param nums the array of positive integers
34-
* @return the length of the smallest subarray with sum ≥ target,
35-
* or 0 if no such subarray exists
36-
*/
37-
public static int findMinSubArrayLen(int target, int[] nums) {
38-
int windowStart = 0;
39-
int windowSum = 0;
40-
int minLength = Integer.MAX_VALUE;
5+
public static int smallestSubarrayLen(int[] arr, int target) {
6+
if (arr == null || arr.length == 0) return 0;
7+
int left = 0, sum = 0, minLen = Integer.MAX_VALUE;
418

42-
for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) {
43-
windowSum += nums[windowEnd];
44-
45-
while (windowSum >= target) {
46-
minLength = Math.min(minLength, windowEnd - windowStart + 1);
47-
windowSum -= nums[windowStart];
48-
windowStart++;
9+
for (int right = 0; right < arr.length; right++) {
10+
sum += arr[right];
11+
while (sum >= target) {
12+
minLen = Math.min(minLen, right - left + 1);
13+
sum -= arr[left++];
4914
}
5015
}
51-
52-
return (minLength == Integer.MAX_VALUE) ? 0 : minLength;
16+
return minLen == Integer.MAX_VALUE ? 0 : minLen;
5317
}
5418
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Unit tests for SmallestSubarrayWithSum algorithm
8+
*/
9+
public class SmallestSubarrayWithSumTest {
10+
11+
@Test
12+
public void testBasicCase() {
13+
int[] arr = {2, 3, 1, 2, 4, 3};
14+
int target = 7;
15+
int expected = 2; // subarray [4,3]
16+
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
17+
}
18+
19+
@Test
20+
public void testNoValidSubarray() {
21+
int[] arr = {1, 1, 1, 1};
22+
int target = 10;
23+
int expected = 0; // no subarray reaches target
24+
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
25+
}
26+
27+
@Test
28+
public void testSingleElement() {
29+
int[] arr = {5};
30+
int target = 5;
31+
int expected = 1;
32+
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
33+
}
34+
35+
@Test
36+
public void testAllElementsSame() {
37+
int[] arr = {2, 2, 2, 2, 2};
38+
int target = 6;
39+
int expected = 3;
40+
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
41+
}
42+
43+
@Test
44+
public void testLargeInput() {
45+
int[] arr = {1, 2, 3, 4, 5, 6};
46+
int target = 11;
47+
int expected = 2; // subarray [5,6]
48+
assertEquals(expected, SmallestSubarrayWithSum.smallestSubarrayLen(arr, target));
49+
}
50+
}

0 commit comments

Comments
 (0)