Skip to content

Arth longest subarray #6458

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion src/main/java/com/thealgorithms/recursion/FibonacciSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ public static int fibonacci(int n) {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}


// n is the number upto which fibonacci sequence is required
// for example n = 4 , output = 0 1 1 2
public static void fibo(int n){
int a = 0 ; int b = 1 ; int c =0;
System.out.print(a+" "+b);
for (int i = 2 ; i<n ; i++){
c = a+b;
System.out.print(" "+ c);
a = b ;
b = c ;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ private LongestSubarrayWithSumLessOrEqualToK() {
* @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
int length = 0 ;
int currSum = 0 ;
int maxLength = Integer.MIN_VALUE;
// inspired by kadane's algorithm
for (int i = 0 ; i<arr.length;i++){
currSum = currSum + arr[i];
length++;
if(currSum==k){
maxLength = Math.max(maxLength, length);
length = 0 ;
currSum = 0 ;
}if(i==arr.length-1){
currSum=0;
length=0;
}

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

}

return maxLength; // Return the maximum length found
}
return maxLength ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public class LongestSubarrayWithSumLessOrEqualToKTest {
* Tests for the longest subarray with a sum less than or equal to k.
*/
@Test
public void testLongestSubarrayWithSumLEK() {
public static 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