mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add Longest Subarray With Sum Less Than or Equal to K algorithm (#6042)
This commit is contained in:
@ -144,7 +144,7 @@ public class BinaryTree {
|
|||||||
if (temp == root) {
|
if (temp == root) {
|
||||||
root = null;
|
root = null;
|
||||||
} // This if/else assigns the new node to be either the left or right child of the
|
} // This if/else assigns the new node to be either the left or right child of the
|
||||||
// parent
|
// parent
|
||||||
else if (temp.parent.data < temp.data) {
|
else if (temp.parent.data < temp.data) {
|
||||||
temp.parent.right = null;
|
temp.parent.right = null;
|
||||||
} else {
|
} else {
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -1,82 +1,78 @@
|
|||||||
package com.thealgorithms.datastructures.trees;
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for the BinaryTree class.
|
||||||
|
*/
|
||||||
public class BinaryTreeTest {
|
public class BinaryTreeTest {
|
||||||
|
|
||||||
// checks that adding populating the tree and searching for data
|
|
||||||
// retrieves the expected data
|
|
||||||
@Test
|
@Test
|
||||||
void test1() {
|
public void testInsertAndFind() {
|
||||||
BinaryTree t = new BinaryTree();
|
BinaryTree tree = new BinaryTree();
|
||||||
t.put(3);
|
tree.put(3);
|
||||||
t.put(5);
|
tree.put(5);
|
||||||
t.put(7);
|
tree.put(7);
|
||||||
t.put(9);
|
tree.put(9);
|
||||||
t.put(12);
|
tree.put(12);
|
||||||
|
|
||||||
assertEquals(t.find(5).data, 5);
|
Assertions.assertNotNull(tree.find(5), "Node with value 5 should exist");
|
||||||
assertEquals(t.find(7).data, 7);
|
Assertions.assertEquals(5, tree.find(5).data, "Value of the found node should be 5");
|
||||||
|
Assertions.assertEquals(7, tree.find(7).data, "Value of the found node should be 7");
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks that removing data from the tree
|
|
||||||
// properly removes and makes the new root the expected new root
|
|
||||||
@Test
|
@Test
|
||||||
void test2() {
|
public void testRemove() {
|
||||||
BinaryTree t = new BinaryTree();
|
BinaryTree tree = new BinaryTree();
|
||||||
t.put(3);
|
tree.put(3);
|
||||||
t.put(5);
|
tree.put(5);
|
||||||
t.put(7);
|
tree.put(7);
|
||||||
t.put(9);
|
tree.put(9);
|
||||||
t.put(12);
|
tree.put(12);
|
||||||
t.remove(3);
|
tree.remove(3);
|
||||||
t.remove(5);
|
tree.remove(5);
|
||||||
t.remove(7);
|
tree.remove(7);
|
||||||
|
|
||||||
// Checks whether the root is null before accessing date
|
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after removals");
|
||||||
if (t.getRoot() != null) {
|
if (tree.getRoot() != null) {
|
||||||
assertEquals(t.getRoot().data, 9);
|
Assertions.assertEquals(9, tree.getRoot().data, "Root value should be 9 after removals");
|
||||||
} else {
|
} else {
|
||||||
fail("The root node is null after removal.");
|
Assertions.fail("Root should not be null after removals, but it is.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks that removing an unexistend node returns false
|
|
||||||
// as specified by the documentation of the function
|
|
||||||
@Test
|
@Test
|
||||||
void test3() {
|
public void testRemoveReturnValue() {
|
||||||
BinaryTree t = new BinaryTree();
|
BinaryTree tree = new BinaryTree();
|
||||||
t.put(3);
|
tree.put(3);
|
||||||
t.put(5);
|
tree.put(5);
|
||||||
t.put(7);
|
tree.put(7);
|
||||||
t.put(9);
|
tree.put(9);
|
||||||
t.put(12);
|
tree.put(12);
|
||||||
|
|
||||||
assertEquals(t.remove(9), true);
|
Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true");
|
||||||
assertEquals(t.remove(398745987), false);
|
Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the bfs, inOrder, preOrder and postOrder functions
|
|
||||||
// worg as expected, also increases the coverage measures in
|
|
||||||
// JaCoCo
|
|
||||||
@Test
|
@Test
|
||||||
void test4() {
|
public void testTraversalMethods() {
|
||||||
BinaryTree t = new BinaryTree();
|
BinaryTree tree = new BinaryTree();
|
||||||
t.put(3);
|
tree.put(3);
|
||||||
t.put(5);
|
tree.put(5);
|
||||||
t.put(7);
|
tree.put(7);
|
||||||
t.put(9);
|
tree.put(9);
|
||||||
t.put(12);
|
tree.put(12);
|
||||||
|
|
||||||
t.bfs(t.find(12));
|
// Testing traversal methods
|
||||||
t.inOrder(t.getRoot());
|
tree.bfs(tree.getRoot());
|
||||||
t.preOrder(t.getRoot());
|
tree.inOrder(tree.getRoot());
|
||||||
t.postOrder(t.getRoot());
|
tree.preOrder(tree.getRoot());
|
||||||
|
tree.postOrder(tree.getRoot());
|
||||||
|
|
||||||
assertEquals(t.remove(9), true);
|
Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true");
|
||||||
assertEquals(t.remove(398745987), false);
|
Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false");
|
||||||
|
|
||||||
|
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after operations");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user