Skip to content

Commit 0560080

Browse files
committed
Format LongestSubarrayWithSumLessOrEqualToK algorithm and tests with clang-format
1 parent acbad2b commit 0560080

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Average performance O(n)
1111
* Worst-case space complexity O(1)
1212
*
13-
* @author (https://github.com/Chiefpatwal)
13+
* @author (https://github.com/Chiefpatwal)
1414
*/
1515
public final class LongestSubarrayWithSumLessOrEqualToK {
1616

@@ -26,23 +26,23 @@ private LongestSubarrayWithSumLessOrEqualToK() {
2626
* @return the length of the longest subarray with sum less than or equal to k
2727
*/
2828
public static int longestSubarrayWithSumLEK(int[] arr, int k) {
29-
int maxLength = 0;
30-
int currentSum = 0;
31-
int left = 0;
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
3232

3333
for (int right = 0; right < arr.length; right++) {
34-
currentSum += arr[right];
34+
currentSum += arr[right]; // Expand the window to the right
3535

3636
// Shrink the window from the left if the current sum exceeds k
37-
while (currentSum > k) {
38-
currentSum -= arr[left];
39-
left++;
37+
while (currentSum > k && left <= right) {
38+
currentSum -= arr[left]; // Remove the leftmost element
39+
left++; // Move the left index to the right
4040
}
4141

4242
// Update maxLength if the current window is valid
4343
maxLength = Math.max(maxLength, right - left + 1);
4444
}
4545

46-
return maxLength;
46+
return maxLength; // Return the maximum length found
4747
}
4848
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
/**
8-
* Unit tests for the LongestSubarrayWithSumLessOrEqualToK class.
9-
*
10-
* @author (https://github.com/Chiefpatwal)
11-
*/
127
public class LongestSubarrayWithSumLessOrEqualToKTest {
13-
148
@Test
159
public void testLongestSubarrayWithSumLEK() {
16-
// Test cases for the longestSubarrayWithSumLEK method
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));
10+
assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 6)); // {1, 2, 3}
11+
assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 10)); // {1, 2, 3, 4}
12+
assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {5, 1, 2, 3}, 5)); // {5}
13+
assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3}, 0)); // No valid subarray
2214
}
2315
}

0 commit comments

Comments
 (0)