Skip to content

Commit ad52f3e

Browse files
committed
Format LongestSubarrayWithSumLessOrEqualToK algorithm and tests with clang-format
1 parent 647c4a8 commit ad52f3e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.thealgorithms.slidingwindow;
22

33
/**
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`.
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.
66
*
77
* <p>
88
* Worst-case performance O(n)
@@ -19,28 +19,27 @@ private LongestSubarrayWithSumLessOrEqualToK() {
1919
}
2020

2121
/**
22-
* Finds the length of the longest subarray with a sum less than or equal to a given value.
22+
* This method finds the length of the longest subarray with a sum less than or equal to k.
2323
*
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
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
2727
*/
28-
public static int longestSubarrayWithSumLEK(int[] nums, int k) {
28+
public static int longestSubarrayWithSumLEK(int[] arr, int k) {
2929
int maxLength = 0;
3030
int currentSum = 0;
3131
int left = 0;
3232

33-
// Expand the window by moving `right`
34-
for (int right = 0; right < nums.length; right++) {
35-
currentSum += nums[right];
33+
for (int right = 0; right < arr.length; right++) {
34+
currentSum += arr[right];
3635

37-
// Shrink the window from the left if `currentSum` exceeds `k`
38-
while (currentSum > k && left <= right) {
39-
currentSum -= nums[left];
36+
// Shrink the window from the left if the current sum exceeds k
37+
while (currentSum > k) {
38+
currentSum -= arr[left];
4039
left++;
4140
}
4241

43-
// Update the maximum length if the current window is valid
42+
// Update maxLength if the current window is valid
4443
maxLength = Math.max(maxLength, right - left + 1);
4544
}
4645

src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class LongestSubarrayWithSumLessOrEqualToKTest {
1414
@Test
1515
public void testLongestSubarrayWithSumLEK() {
1616
// 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));
17+
assertEquals(5, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{1, 2, 3, 4, 5}, 11));
18+
assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{1, 2, 3, 4, 5}, 7));
19+
assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{1, 2, 3, 4, 5}, 3));
20+
assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{}, 0));
21+
assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[]{2, 1, 5, 2, 3, 2}, 7));
2222
}
2323
}

0 commit comments

Comments
 (0)