Max Sum Subarray of size K | Practice | GeeksforGeeks

Given an array of integers Arr of size N and a number K. Return the maximum sum of a subarray of size K.
 
Example 1:
Input:
N = 4, K = 2
Arr = [100, 200, 300, 400]
Output:
700
Explanation:
Arr3  + Arr4 =700,
which is maximum.
 
Example 2:
Input:
N = 4, K = 4
Arr = [100, 200, 300, 400]
Output:
1000
Explanation:
Arr1 + Arr2 + Arr3  
+ Arr4 =1000,
which is maximum.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumSubarray() which takes the integer k, vector Arr with size N, containing the elements of the array and returns the maximum sum of a subarray of size K.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 

Constraints:
1<=N<=105
1<=K<=N
 
0
vickysah2 days ago
class Solution{   public:   int maximumSumSubarray(int K, vector<int> &Arr , int N){             int temp=Arr[0];          Arr[0]=0;       for(int i=1;i<N;i++)       {           int temp2=Arr[i];           Arr[i]=temp+Arr[i-1];           temp=temp2;       }              Arr.push_back(temp+Arr[i-1]);                          int a=0;       for(int i=0;i<N-K+1;i++)       {           a=max(a , Arr[K+i]-Arr[i]);       }       return a;   }};
 
+1
shahilsingh4562 weeks ago
         EASIEST SOLUTION C++
 
 int maximumSumSubarray(int K, vector<int> &Arr , int N){       // code here        int sum=0;        for(int i=0;i<K;i++)        {            sum+=Arr[i];        }       int  curr_sum=sum;        for(int i=0;i<N-K;i++)        {            sum=sum-Arr[i]+Arr[i+K];            curr_sum=max(curr_sum,sum);        }        return curr_sum;   }
+1
himanshu32492 weeks ago
class Solution{   
public:
    int maximumSumSubarray(int k, vector<int> &nums , int n){
        // code here
        int i(0), j(0), mx = 0, sum = 0;
        
        while(j < n){
            // first calculation to add the effect of the second pointer
            sum += nums[j];
            
            if(j - i + 1 == k){
                mx = max(mx, sum);
                
                // calculation to remove the effect of the first pointer
                sum -= nums[i];
                i++;
            }
            
            j++;
        }
        
        return mx;
    }
};
0
19bcs13143 weeks ago
class Solution{
    static int maximumSumSubarray(int k, ArrayList<Integer> arr,int n){
        // code here
        
        int i=0,j=0,sum=0,max=-1;
        
        while(j<n){
            sum+=arr.get(j);
            if(j-i+1<k){
                j++;
            }else if(j-i+1==k){
                max=Math.max(sum,max);
                sum-=arr.get(i);
                i++;j++;
            }
        }
        return max;
    }
}
+1
rajatgupta96963 weeks ago
// Using Sliding WIndow concept
    int maximumSumSubarray(int K, vector<int> &Arr , int N){
       int i=0,j=0;
       int sum=0;
       int maxSum = INT_MIN;
       while(j<N){
           sum+=Arr[j];
           if(j-i+1<K)
           {
               j++;
           }
           else{
               maxSum =max(maxSum,sum);
               
               // reduce arr[i];
               sum-=Arr[i];
               i++;
               j++;
           }
       }
       return maxSum;
    }
+1
shahabuddinbravo403 weeks ago
 // int i=0,j=0,mx=INT_MIN,sum=0;              // while(j<Arr.size())       // {       //     sum=sum+Arr[j];                  //     if((j-i+1)<K){       //         j++;       //     }       //     else if((j-i+1)==K){       //       mx=max(mx,sum);       //       sum=sum-Arr[i];       //       i++;       //       j++;       //     }       // }       // return mx;              int i=0,j=0,mx=INT_MIN,sum=0;              for(int j=0;j<Arr.size();j++){                      sum=sum+Arr[j];                      if((j-i+1)==K){               mx=max(mx,sum);               sum=sum-Arr[i];               i++;           }       }       return mx;   }
0
minturajmdb19991 month ago
int maximumSumSubarray(int K, vector<int> &Arr , int N)   {       int i=0,j=0;       int sum=0,maxx=0;       while(Arr.size()>j)        {                       sum=sum+Arr[j];                        if(j-i+1<K)                 j++;                          else if(j-i+1==K)              {                  maxx=max(sum,maxx);                                   sum=sum-Arr[i];                  i++;                  j++;              }                      }           return maxx;   }
+1
swastikp17111 month ago
Simple Java Solution using sliding window
Satisfies All Kind Of Test cases ( Standard Solution )
class Solution{
    static int maximumSumSubarray(int k, ArrayList<Integer> arr,int n){
        
        int sum=0;
        int maxSum=Integer.MIN_VALUE;
        
        for(int i=0;i<n;i++){
            
            // Subtracting Element which is not present in window
            if(i>=k){
                sum-=arr.get(i-k);
            }
            
            //Adding element to sum(to slide window forward)
            sum+=arr.get(i);
            
            //condition to check whether sum is greater than maxSum or not
            if(i>=k-1){
                maxSum=Math.max(maxSum,sum);
            }
        }
        return maxSum;
    }
}
0
akashgahlot19261 month ago
nums[k]=max;k++;
0
zacksuhail1 month ago
EASIEST JAVA SOLUTION:
 static int maximumSumSubarray(int K, ArrayList<Integer> Arr,int N){       // code here       int max = 0;       int sum = 0;       int start = 0;       for(int i = 0 ; i < N ; i++){           sum += Arr.get(i);                      if(i >= K){               sum -= Arr.get(start);               start++;           }           max = Math.max(sum , max);       }       return max;   }
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.