Skip to content

Commit 44eca38

Browse files
committed
Add Longest Subarray with Sum Less Than or Equal to K algorithm and tests
1 parent a163816 commit 44eca38

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
/**
4+
* The Longest Subarray with Sum Less Than or Equal to K algorithm finds the length of the longest
5+
* subarray with a sum that does not exceed a given value `k`.
6+
*
7+
* <p>
8+
* Worst-case performance O(n)
9+
* Best-case performance O(n)
10+
* Average performance O(n)
11+
* Worst-case space complexity O(1)
12+
*
13+
* @author (https://github.com/Chiefpatwal)
14+
*/
15+
public final class LongestSubarrayWithSumLessOrEqualToK {
16+
17+
// Prevent instantiation
18+
private LongestSubarrayWithSumLessOrEqualToK() {
19+
}
20+
21+
/**
22+
* Finds the length of the longest subarray with a sum less than or equal to a given value.
23+
*
24+
* @param nums The input array of integers
25+
* @param k The maximum allowed sum for the subarray
26+
* @return The length of the longest subarray with sum <= k
27+
*/
28+
public static int longestSubarrayWithSumLEK(int[] nums, int k) {
29+
int maxLength = 0;
30+
int currentSum = 0;
31+
int left = 0;
32+
33+
// Expand the window by moving `right`
34+
for (int right = 0; right < nums.length; right++) {
35+
currentSum += nums[right];
36+
37+
// Shrink the window from the left if `currentSum` exceeds `k`
38+
while (currentSum > k && left <= right) {
39+
currentSum -= nums[left];
40+
left++;
41+
}
42+
43+
// Update the maximum length if the current window is valid
44+
maxLength = Math.max(maxLength, right - left + 1);
45+
}
46+
47+
return maxLength;
48+
}
49+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the LongestSubarrayWithSumLessOrEqualToK class.
9+
*
10+
* @author (https://github.com/Chiefpatwal)
11+
*/
12+
public class LongestSubarrayWithSumLessOrEqualToKTest {
13+
14+
@Test
15+
public void testLongestSubarrayWithSumLEK() {
16+
// Test cases for the longestSubarrayWithSumLEK method
17+
assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{1, 2, 3, 4, 5}, 11));
18+
assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{2, 1, 1, 1, 1}, 3));
19+
assertEquals(5, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{1, 2, 3, 4, 5}, 15));
20+
assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{10, 20, 30}, 5));
21+
assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{3, 1, 2, 1, 1}, 5));
22+
}
23+
}

0 commit comments

Comments
 (0)