From 6085b1f54647ae5f16d3d86caa4c78965852a876 Mon Sep 17 00:00:00 2001 From: ArthBachhuka123 Date: Sat, 2 Aug 2025 20:44:40 +0530 Subject: [PATCH 1/2] new method without recursion --- .../recursion/FibonacciSeries.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java b/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java index e5f474085367..2b5772a65a7a 100644 --- a/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java +++ b/src/main/java/com/thealgorithms/recursion/FibonacciSeries.java @@ -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 Date: Mon, 4 Aug 2025 12:11:58 +0530 Subject: [PATCH 2/2] added new method --- .../LongestSubarrayWithSumLessOrEqualToK.java | 34 +++++++++---------- ...gestSubarrayWithSumLessOrEqualToKTest.java | 3 +- 2 files changed, 19 insertions(+), 18 deletions(-) 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