Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thealgorithms</groupId>
<artifactId>Java</artifactId>
Expand Down Expand Up @@ -123,9 +123,9 @@
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.18.2</version>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.18.2</version>
</dependency>
</dependencies>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.slidingwindow;

/**
* The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length
* of the longest subarray whose sum is less than or equal to a given value k.
*
* <p>
* Worst-case performance O(n)
* Best-case performance O(n)
* Average performance O(n)
* Worst-case space complexity O(1)
*
* @author (https://github.com/Chiefpatwal)
*/
public final class LongestSubarrayWithSumLessOrEqualToK {

// Prevent instantiation
private LongestSubarrayWithSumLessOrEqualToK() {
}

/**
* This method finds the length of the longest subarray with a sum less than or equal to k.
*
* @param arr is the input array
* @param k is the maximum sum allowed
* @return the length of the longest subarray with sum less than or equal to k
*/
public static int longestSubarrayWithSumLEK(int[] arr, int k) {
int maxLength = 0; // To store the maximum length found
int currentSum = 0; // To store the current sum of the window
int left = 0; // Left index of the sliding window

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

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

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

return maxLength; // Return the maximum length found
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.thealgorithms.slidingwindow;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Unit tests for the LongestSubarrayWithSumLessOrEqualToK algorithm.
*/
public class LongestSubarrayWithSumLessOrEqualToKTest {

/**
* Tests for the longest subarray with a sum less than or equal to k.
*/
@Test
public void testLongestSubarrayWithSumLEK() {
assertEquals(3, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 6)); // {1, 2, 3}
assertEquals(4, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3, 4}, 10)); // {1, 2, 3, 4}
assertEquals(2, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {5, 1, 2, 3}, 5)); // {5}
assertEquals(0, LongestSubarrayWithSumLessOrEqualToK.longestSubarrayWithSumLEK(new int[] {1, 2, 3}, 0)); // No valid subarray
}
}
Loading