Skip to content

Add Longest Subarray With Sum Less Than or Equal to K algorithm and t… #6042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public boolean remove(int value) {
if (temp == root) {
root = null;
} // 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) {
temp.parent.right = null;
} else {
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
@@ -1,82 +1,78 @@
package com.thealgorithms.datastructures.trees;

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the BinaryTree class.
*/
public class BinaryTreeTest {

// checks that adding populating the tree and searching for data
// retrieves the expected data
@Test
void test1() {
BinaryTree t = new BinaryTree();
t.put(3);
t.put(5);
t.put(7);
t.put(9);
t.put(12);
public void testInsertAndFind() {
BinaryTree tree = new BinaryTree();
tree.put(3);
tree.put(5);
tree.put(7);
tree.put(9);
tree.put(12);

assertEquals(t.find(5).data, 5);
assertEquals(t.find(7).data, 7);
Assertions.assertNotNull(tree.find(5), "Node with value 5 should exist");
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
void test2() {
BinaryTree t = new BinaryTree();
t.put(3);
t.put(5);
t.put(7);
t.put(9);
t.put(12);
t.remove(3);
t.remove(5);
t.remove(7);
public void testRemove() {
BinaryTree tree = new BinaryTree();
tree.put(3);
tree.put(5);
tree.put(7);
tree.put(9);
tree.put(12);
tree.remove(3);
tree.remove(5);
tree.remove(7);

// Checks whether the root is null before accessing date
if (t.getRoot() != null) {
assertEquals(t.getRoot().data, 9);
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after removals");
if (tree.getRoot() != null) {
Assertions.assertEquals(9, tree.getRoot().data, "Root value should be 9 after removals");
} 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
void test3() {
BinaryTree t = new BinaryTree();
t.put(3);
t.put(5);
t.put(7);
t.put(9);
t.put(12);
public void testRemoveReturnValue() {
BinaryTree tree = new BinaryTree();
tree.put(3);
tree.put(5);
tree.put(7);
tree.put(9);
tree.put(12);

assertEquals(t.remove(9), true);
assertEquals(t.remove(398745987), false);
Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true");
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
void test4() {
BinaryTree t = new BinaryTree();
t.put(3);
t.put(5);
t.put(7);
t.put(9);
t.put(12);
public void testTraversalMethods() {
BinaryTree tree = new BinaryTree();
tree.put(3);
tree.put(5);
tree.put(7);
tree.put(9);
tree.put(12);

// Testing traversal methods
tree.bfs(tree.getRoot());
tree.inOrder(tree.getRoot());
tree.preOrder(tree.getRoot());
tree.postOrder(tree.getRoot());

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

assertEquals(t.remove(9), true);
assertEquals(t.remove(398745987), false);
Assertions.assertNotNull(tree.getRoot(), "Root should not be null after operations");
}
}
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
}
}