Skip to content

Commit 3c9aac6

Browse files
committed
Add Longest Subarray With Sum Less Than or Equal to K algorithm and tests
1 parent a163816 commit 3c9aac6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
/**
4+
* The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length
5+
* of the longest subarray whose sum is less than or equal to 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+
* This method finds the length of the longest subarray with a sum less than or equal to k.
23+
*
24+
* @param arr is the input array
25+
* @param k is the maximum sum allowed
26+
* @return the length of the longest subarray with sum less than or equal to k
27+
*/
28+
public static int longestSubarrayWithSumLEK(int[] arr, int k) {
29+
int maxLength = 0; // To store the maximum length found
30+
int currentSum = 0; // To store the current sum of the window
31+
int left = 0; // Left index of the sliding window
32+
33+
for (int right = 0; right < arr.length; right++) {
34+
currentSum += arr[right]; // Expand the window to the right
35+
36+
// Shrink the window from the left if the current sum exceeds k
37+
while (currentSum > k && left <= right) {
38+
currentSum -= arr[left]; // Remove the leftmost element
39+
left++; // Move the left index to the right
40+
}
41+
42+
// Update maxLength if the current window is valid
43+
maxLength = Math.max(maxLength, right - left + 1);
44+
}
45+
46+
return maxLength; // Return the maximum length found
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 algorithm.
9+
*/
10+
public class LongestSubarrayWithSumLessOrEqualToKTest {
11+
12+
/**
13+
* Tests for the longest subarray with a sum less than or equal to k.
14+
*/
15+
@Test
16+
public void testLongestSubarrayWithSumLEK() {
17+
assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 6)); // {1, 2, 3}
18+
assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 10)); // {1, 2, 3, 4}
19+
assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {5, 1, 2, 3}, 5)); // {5}
20+
assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3}, 0)); // No valid subarray
21+
}
22+
}

0 commit comments

Comments
 (0)