Maximum sum of subarray less than or equal to x | Practice | GeeksforGeeks

Given an array arr[] of integers of size N and a number X, the task is to find the sum of subarray having maximum sum less than or equal to the given value of X.
Example 1:
Input: N = 5, X = 11
arr[] = {1, 2, 3, 4, 5} 
Output:  10
Explanation: Subarray having maximum 
sum is {1, 2, 3, 4}.
 
Example 2:
Input: N = 5, X = 7
arr[] = {2, 4, 6, 8, 10} 
Output:  6
Explanation: Subarray having maximum 
sum is {2, 4} or {6}.
 
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function calculateMaxSumLength() that takes array arr, integer N, and integer X as parameters and returns maximum sum of any subarray that is less than or equal to x.
 
Expected Time Complexity: O(N). 
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 106
0
bharshit4682 months ago
Easy to Understand | cpp
class Solution{
public:int findMaxSubarraySum(long long arr[], int n, long long k){    int i = 0 , j = 0 , maxSum = 0 , sum = 0 ;        while(j < n)    {        sum +=arr[j];                if(sum <= k)            maxSum = max(maxSum , sum);                     else if(sum > k)         {             while(sum > k)             {                 sum -=arr[i];                 i++;             }                          if(sum <= k)                maxSum = max(maxSum , sum);         }        j++;    }    return maxSum;}  
 
}; 
 
0
kake13373 months ago
    /*Its O(N) why?
    because every element gets add at most one time and gets removed from "s" at most one time, So the time complexity will be O(2N) which is linear!*/
    
    int i=0;
    long long s=0,r=0;
    for(int j=0;j<n;j++)
    {
        s+=arr[j];
        while(s>sum)
        {
            s-=arr[i];
            i++;
        }
        r=max(r,s);
    }
    return r;
+1
shyamprakash8073 months ago
time complexity : 0.4/5.1
Pyhton solution using Sliding window Approach
class Solution:   def findMaxSubarraySum(self, arr, n, k):       # Your code goes here       sum = 0       ans = 0       i, j = 0, 0       while j < n:           sum += arr[j]           if sum < k:               ans = max(ans, sum)               j += 1           elif sum == k:               return k           else:               while sum > k:                   sum -= arr[i]                   i += 1               ans = max(ans, sum)               j += 1       return ans
+2
krishnarajput10274 months ago
int findMaxSubarraySum(long long arr[], int n, long long sum){    int j=0;    int ans=INT_MIN;    int s=0;    for(int i=0;i<n;i++){        s+=arr[i];        while(s>sum){            s-=arr[j];            j++;        }        ans=max(s,ans);    }    return ans;}  
0
shubodeepbera7 months ago
 int i=0,j=0,curr_sum=0; int ans=INT_MIN; while(i<n){     curr_sum+=arr[i];     i++;          while(curr_sum>sum){         curr_sum-=arr[j];         j++;     }     ans=max(ans,curr_sum); } return ans;
0
delectable_boomer
This comment was deleted.
+1
Zilen Modi10 months ago
Zilen Modi
Consider this test case:3-1 7 117
right maximum sum=>7but, sliding Window giving =>6Add this test case.
0
Om Prakash10 months ago
Om Prakash
I thing Something is wrong with JAVA's Driver Code
0
Imran Wahid10 months ago
Imran Wahid
Easy C++ solution in O(n) TC and O(1) SChttps://ide.geeksforgeeks.o...
0
Aniket patel11 months ago
Aniket patel
class Solution{public:int findMaxSubarraySum(long long arr[], int n, long long sum){    int l=0,r=0;    long long curr=arr[0],max=INT_MIN;    while(r<n) {="" if(max<curr="" and="" curr<sum)="" max="curr;" if(curr="">sum)        {            curr-=arr[l];            l++;        }        else if(curr<sum) {="" if(r!="n-1)" {="" r++;="" curr+="arr[r];" }="" else="" break;="" }="" else="" (sum="=x)" return="" sum;="" }="" return="" max;="" }="" };="" https:="" practice.geeksforgeeks.org="" viewsol.php?subid="b36a06fad1b7f8ad91b5f81380720484&amp;pid=703423&amp;user=aniket66patel">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.