Skip to content

Commit 11cbdbf

Browse files
committed
feat(slidingwindow): add SmallestSubarrayWithSum and its tests
1 parent bb6385e commit 11cbdbf

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
/**
4+
* Finds the length of the smallest contiguous subarray
5+
* whose sum is greater than or equal to a given target.
6+
*
7+
* <p>Example:
8+
* arr = [2, 3, 1, 2, 4, 3], target = 7
9+
* Smallest subarray = [4, 3], length = 2
10+
*
11+
* <p>Time complexity: O(n)
12+
* Space complexity: O(1)
13+
*/
14+
public final class SmallestSubarrayWithSum {
15+
16+
// Prevent instantiation
17+
private SmallestSubarrayWithSum() {}
18+
19+
/**
20+
* Returns the minimal length of a contiguous subarray of which
21+
* the sum ≥ target. If no such subarray exists, returns 0.
22+
*
23+
* @param arr the input array
24+
* @param target the target sum
25+
* @return the minimal subarray length, or 0 if not found
26+
*/
27+
public static int smallestSubarrayLen(int[] arr, int target) {
28+
if (arr == null || arr.length == 0) {
29+
return 0;
30+
}
31+
32+
int left = 0;
33+
int sum = 0;
34+
int minLen = Integer.MAX_VALUE;
35+
36+
for (int right = 0; right < arr.length; right++) {
37+
sum += arr[right];
38+
while (sum >= target) {
39+
minLen = Math.min(minLen, right - left + 1);
40+
sum -= arr[left++];
41+
}
42+
}
43+
44+
return minLen == Integer.MAX_VALUE ? 0 : minLen;
45+
}
46+
}
47+
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 {@link SmallestSubarrayWithSum}.
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;
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)