Skip to content

Commit bca8d0e

Browse files
authored
Add Longest Subarray With Sum Less Than or Equal to K algorithm (TheAlgorithms#6042)
1 parent a2e526b commit bca8d0e

File tree

4 files changed

+124
-58
lines changed

4 files changed

+124
-58
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public boolean remove(int value) {
144144
if (temp == root) {
145145
root = null;
146146
} // This if/else assigns the new node to be either the left or right child of the
147-
// parent
147+
// parent
148148
else if (temp.parent.data < temp.data) {
149149
temp.parent.right = null;
150150
} else {
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: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,78 @@
11
package com.thealgorithms.datastructures.trees;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.fail;
5-
3+
import org.junit.jupiter.api.Assertions;
64
import org.junit.jupiter.api.Test;
75

6+
/**
7+
* Unit tests for the BinaryTree class.
8+
*/
89
public class BinaryTreeTest {
910

10-
// checks that adding populating the tree and searching for data
11-
// retrieves the expected data
1211
@Test
13-
void test1() {
14-
BinaryTree t = new BinaryTree();
15-
t.put(3);
16-
t.put(5);
17-
t.put(7);
18-
t.put(9);
19-
t.put(12);
12+
public void testInsertAndFind() {
13+
BinaryTree tree = new BinaryTree();
14+
tree.put(3);
15+
tree.put(5);
16+
tree.put(7);
17+
tree.put(9);
18+
tree.put(12);
2019

21-
assertEquals(t.find(5).data, 5);
22-
assertEquals(t.find(7).data, 7);
20+
Assertions.assertNotNull(tree.find(5), "Node with value 5 should exist");
21+
Assertions.assertEquals(5, tree.find(5).data, "Value of the found node should be 5");
22+
Assertions.assertEquals(7, tree.find(7).data, "Value of the found node should be 7");
2323
}
2424

25-
// checks that removing data from the tree
26-
// properly removes and makes the new root the expected new root
2725
@Test
28-
void test2() {
29-
BinaryTree t = new BinaryTree();
30-
t.put(3);
31-
t.put(5);
32-
t.put(7);
33-
t.put(9);
34-
t.put(12);
35-
t.remove(3);
36-
t.remove(5);
37-
t.remove(7);
26+
public void testRemove() {
27+
BinaryTree tree = new BinaryTree();
28+
tree.put(3);
29+
tree.put(5);
30+
tree.put(7);
31+
tree.put(9);
32+
tree.put(12);
33+
tree.remove(3);
34+
tree.remove(5);
35+
tree.remove(7);
3836

39-
// Checks whether the root is null before accessing date
40-
if (t.getRoot() != null) {
41-
assertEquals(t.getRoot().data, 9);
37+
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after removals");
38+
if (tree.getRoot() != null) {
39+
Assertions.assertEquals(9, tree.getRoot().data, "Root value should be 9 after removals");
4240
} else {
43-
fail("The root node is null after removal.");
41+
Assertions.fail("Root should not be null after removals, but it is.");
4442
}
4543
}
4644

47-
// checks that removing an unexistend node returns false
48-
// as specified by the documentation of the function
4945
@Test
50-
void test3() {
51-
BinaryTree t = new BinaryTree();
52-
t.put(3);
53-
t.put(5);
54-
t.put(7);
55-
t.put(9);
56-
t.put(12);
46+
public void testRemoveReturnValue() {
47+
BinaryTree tree = new BinaryTree();
48+
tree.put(3);
49+
tree.put(5);
50+
tree.put(7);
51+
tree.put(9);
52+
tree.put(12);
5753

58-
assertEquals(t.remove(9), true);
59-
assertEquals(t.remove(398745987), false);
54+
Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true");
55+
Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false");
6056
}
6157

62-
// check if the bfs, inOrder, preOrder and postOrder functions
63-
// worg as expected, also increases the coverage measures in
64-
// JaCoCo
6558
@Test
66-
void test4() {
67-
BinaryTree t = new BinaryTree();
68-
t.put(3);
69-
t.put(5);
70-
t.put(7);
71-
t.put(9);
72-
t.put(12);
59+
public void testTraversalMethods() {
60+
BinaryTree tree = new BinaryTree();
61+
tree.put(3);
62+
tree.put(5);
63+
tree.put(7);
64+
tree.put(9);
65+
tree.put(12);
66+
67+
// Testing traversal methods
68+
tree.bfs(tree.getRoot());
69+
tree.inOrder(tree.getRoot());
70+
tree.preOrder(tree.getRoot());
71+
tree.postOrder(tree.getRoot());
7372

74-
t.bfs(t.find(12));
75-
t.inOrder(t.getRoot());
76-
t.preOrder(t.getRoot());
77-
t.postOrder(t.getRoot());
73+
Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true");
74+
Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false");
7875

79-
assertEquals(t.remove(9), true);
80-
assertEquals(t.remove(398745987), false);
76+
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after operations");
8177
}
8278
}
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy