diff --git a/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java b/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java index e5f474085367..4f4690825be9 100644 --- a/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java +++ b/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java @@ -11,11 +11,14 @@ public final class FibonacciSeries { private FibonacciSeries() { throw new UnsupportedOperationException("Utility class"); } - public static int fibonacci(int n) { - if (n <= 1) { - return n; - } else { - return fibonacci(n - 1) + fibonacci(n - 2); + public static void fibonacci(int n,int i , int a , int b) { + if(i==n){ + return ; } + int c = a+b ; + System.out.println(" " + c + " "); + fibonacci(n, i+1, b, c); } -} + + +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java b/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java index 55c3f709b467..e862833535e6 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java +++ b/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java @@ -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