-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Find Longest Subarray Sum Is Less Or Equal K Problem (#16)
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
algorithms-and-data-structures/src/main/java/com/xtenzq/arrays/SlidingWindow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.xtenzq.arrays; | ||
|
||
public class SlidingWindow { | ||
|
||
/** | ||
* Finds the length of the longest subarray with a sum less than or equal to a given value. | ||
* | ||
* @param nums the array of positive integers | ||
* @param k the maximum allowed sum of the subarray | ||
* @return the length of the longest subarray with a sum less than or equal to {@code k} | ||
* @implNote This method runs in {@code O(n)} time complexity and {@code O(1)} space complexity, | ||
* where {@code n} is the length of {@code nums}. | ||
*/ | ||
public static int findLongestSubArraySumLessOrEqualK(int[] nums, int k) { | ||
int left = 0, answer = 0, curr = 0; | ||
for (int right = 0; right < nums.length; right++) { | ||
curr += nums[right]; | ||
while (curr > k) { | ||
curr -= nums[left]; | ||
left++; | ||
} | ||
answer = Math.max(answer, right - left + 1); | ||
} | ||
return answer; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
algorithms-and-data-structures/src/test/java/com/xtenzq/arrays/SlidingWindowTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.xtenzq.arrays; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.xtenzq.arrays.SlidingWindow.findLongestSubArraySumLessOrEqualK; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class SlidingWindowTest { | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_Example() { | ||
int[] nums = {3, 2, 1, 3, 1, 1}; | ||
int k = 5; | ||
assertEquals(3, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_AllElementsLessThanK() { | ||
int[] nums = {1, 2, 1, 2, 1}; | ||
int k = 5; | ||
assertEquals(3, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_SingleElementEqualsK() { | ||
int[] nums = {5}; | ||
int k = 5; | ||
assertEquals(1, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_SingleElementGreaterThanK() { | ||
int[] nums = {6}; | ||
int k = 5; | ||
assertEquals(0, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_MultipleSubarrays() { | ||
int[] nums = {1, 2, 3, 4, 5}; | ||
int k = 5; | ||
assertEquals(2, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
|
||
@Test | ||
void testFindLongestSubArraySumLessOrEqualK_EmptyArray() { | ||
int[] nums = {}; | ||
int k = 5; | ||
assertEquals(0, findLongestSubArraySumLessOrEqualK(nums, k)); | ||
} | ||
} |