Max Length of Subarray with Given Sum Limit in JavaScript Array Last Updated : 06 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array, our task is to find the maximum length of a subarray whose sum does not exceed a given value. We can use different approaches like the Brute Force Approach and the Sliding Window Approach to find the maximum length of a subarray. Below are the approaches to find the maximum length of a subarray with a sum not exceeding a given value: Table of Content Using the Brute Force ApproachUsing Sliding Window ApproachUsing the Brute Force ApproachIn this approach, the code employs a brute force method to find the maximum length of a subarray in JavaScript. It iterates through each possible subarray, calculating the sum and updating the maximum length if the sum does not exceed a given value k. Finally, the method returns the maximum length of the subarray with a sum not exceeding k. Example: The example uses the Brute Force method to find the maximum length with the sum not exceeding a given value. JavaScript function maxSubarrayLengthBruteForce(arr, k) { let maxLength = 0; for (let start = 0; start < arr.length; start++) { let sum = 0; for (let end = start; end < arr.length; end++) { sum += arr[end]; if (sum <= k) { maxLength = Math.max(maxLength, end - start + 1); } } } return maxLength; } const array1 = [1, 2, 3, 4, 5]; const k1 = 11; console.log(maxSubarrayLengthBruteForce(array1, k1)); const array2 = [3, 1, 2, 1]; const k2 = 4; console.log(maxSubarrayLengthBruteForce(array2, k2)); Output4 3 Time Complexity: O(n^2) Space Complexity: O(1) Using the Sliding Window ApproachIn this approach, the sliding window technique is used to find the maximum subarray length whose sum doesn't exceed a given value k. It iterates through the array, adjusting the window boundaries to maintain the sum within k. Finally, it returns the maximum subarray length found. Example: The example uses a sliding window technique to find the maximum subarray length with a sum not exceeding a given value. JavaScript function maxSubarrayLengthSlidingWindow(arr, k) { let maxLength = 0; let sum = 0; let start = 0; for (let end = 0; end < arr.length; end++) { sum += arr[end]; while (sum > k) { sum -= arr[start]; start++; } maxLength = Math.max(maxLength, end - start + 1); } return maxLength; } const array3 = [1, 2, 3, 4, 5]; const k3 = 11; console.log(maxSubarrayLengthSlidingWindow(array3, k3)); const array4 = [3, 1, 2, 1]; const k4 = 4; console.log(maxSubarrayLengthSlidingWindow(array4, k4)); Output4 3 Time Complexity: O(n) Space Complexity: O(1) Longest Subarray with given Sum Visit Course Comment More infoAdvertise with us Next Article Longest Subarray with Sum Divisible by Given Number in Array with JavaScript H heysaiyad Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads Longest Subarray with Sum Divisible by Given Number in Array with JavaScript Given an array of integers, the task is to find the longest subarray whose sum is divisible by a given number. Example: Input: [4, 5, 0, -2, -3, 1], k = 5Output: Length = 6Below are the approaches to find the longest subarray with sum divisible by a given number using JavaScript: Table of Content Br 3 min read JavaScript Program to Find Largest Subarray with a Sum Divisible by k Finding the largest subarray with a sum divisible by a given integer 'k' is a common problem in JavaScript and other programming languages. This task involves identifying a contiguous subarray within an array of integers such that the sum of its elements is divisible by 'k' and is as long as possibl 5 min read Length of the Longest Subarray with Zero Sum using JavaScript JavaScript allows us to find the longest subarray with zero-sum. We have to return the length of the longest subarray whose sum is equal to zero. There are several approaches in JavaScript to achieve this which are as follows: Table of Content Using Nested loop (Brute force Approach)Using map object 3 min read JavaScript Program to Find the length of the Longest Continuous Decreasing Subarray Given an array of numbers, our task is to find the length of the longest continuous subarray where the numbers are strictly decreasing. Example: Input:const arr = [7, 4, 7, 6, 5, 4, 6, 8, 9]Output: 4Explanation: The longest continuous decreasing subarray in this array is [7, 6, 5, 4], and its length 3 min read JavaScript Program for K-th Largest Sum Contiguous Subarray In this article, we are going to learn about K-th Largest Sum Contiguous Subarray in JavaScript. K-th Largest Sum Contiguous Subarray refers to finding the K-th largest sum among all possible contiguous subarrays within a given array of numbers, It involves exploring different subarray lengths and p 7 min read JavaScript Program to Find Maximum of Minimum for Every Window Size in a Given Array Given an array of integers, find the maximum of minimums for every window size in the array. The window size varies from 1 to the size of the array. Example: Input: Array: [1, 2, 3, 5, 1, 7, 3]Output: [7, 3, 2, 1, 1, 1, 1]Explanation: The first element in the output indicates the maximum of minimums 4 min read Like