K Subarray Sum | Practice | GeeksforGeeks

Given an integer array arr of size N and two integers K and M, the task is to find M largest sums of K sized subarrays.  
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains three integers N, K, and M.
3. The next line contains N space-separated integers. 

Output: For each test case, print M space-separated integers (Print the sums from the maximum to minimum)

Constraints:
1. 1 <= T <= 100
2. 1 <= K <= N <= 104
3. 1 <= M <= N - K + 1
4. 1 <= arr[i] <= 105

Example:
Input:
2
5 2 3
10 11 10 11 12
5 5 1
1 2 3 4 5 
 
Output:
23 21 21
15

Explanation:
Test Case 1: 2 sized subarray sum are {21, 21, 21, 21, 23}, We need to print 3 largest sums from maximum to minimum
0
lindan1233 months ago
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--)
	{
	    int n,k,m;
	    cin>>n>>k>>m;
	    vector<int> arr(n);
	    for(int i=0;i<n;i++)
	    {
	        cin>>arr[i];
	    }
	    
	    if(n==1)
	    {
	        cout<<arr[0];
	    }
	    else{
	    int i=0;
	    int j=0;
	    int c=0;
	    int sum=0;
	    vector<int> temp;
	    while(j<n)
	    {
	        sum+=arr[j];
	        c++;
	        if(c==k)
	        {
	            temp.push_back(sum);
	            sum = sum-arr[i];
	            i++;
	            c--;
	        }
	        j++;
	        
	    }
	    
	    
	    sort(temp.rbegin(),temp.rend());
	    for(j=0;j<temp.size();j++)
	    {
	        if(m>=j+1)
	        {
	            cout<<temp[j]<<" ";
	        }
	    }
	    }
	    cout<<endl;
	}
	return 0;
}
Time Taken : 0.1sec
Cpp
+1
avinav26113 months ago
Easy C++ Solution !!Working!!
(minimum time and space complexity)
 
-1
naitikrajyaguru3 months ago
it's pizza time
 
#include <bits/stdc++.h>using namespace std;
int main() {//codeint t;cin>>t;while(t--){    int n,k,m;    cin>>n>>k>>m;    int a[n],i;    for(i=0; i<=n-1; i++){        cin>>a[i];    }    int sum[n]={0};    for(i=0; i<=n-k; i++){        for(int j=i; j<=i+k-1; j++){            sum[i]+=a[j];        }    }    sort(sum,sum+i);    for(int j=i-1; j>=i-m; j--){        cout<<sum[j]<<" ";    }    cout<<endl;}return 0;}
-1
vishallodha20093 months ago
Easy c++ solution
 
#include <bits/stdc++.h>
using namespace std;

vector<int> kSubArrSum(int arr[],int n,int k,int m){
    vector<int> ans;
    for(int i=0;i<=n-k;i++){
        int sum = 0;
        for(int j=i;j<i+k;j++){
            sum+=arr[j];
        }
        ans.push_back(sum);
    }
    
    sort(ans.begin(),ans.end(),::greater<int>());
    vector<int> result;
    for(int i=0;i<m;i++){
        result.push_back(ans[i]);
    }
    return result;
}

int main() {
	//code
	int T;
	cin >> T;
	for(int i=0;i<T;i++){
	    int n,k,m;
	    cin >> n >> k >> m;
	    int arr[n];
	    for(int i=0;i<n;i++){
	        cin >> arr[i];
	    }
	    vector<int> ans = kSubArrSum(arr,n,k,m);
	    for(int i=0;i<m;i++){
	        cout<<ans[i]<<" ";
	    }
	    cout<<endl;
	}
	return 0;
}
0
lokendrabaghel0987653 months ago
//JAVA SOLUTION
CAN ANYONE HELP ME TO FIND THE TC & SC OF THIS PROBLEM?
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args){    Scanner scan = new Scanner(System.in);    int T = scan.nextInt();    while(T-- > 0){                int N = scan.nextInt();        int K = scan.nextInt();        int M = scan.nextInt();                int arr[] = new int[N];        for(int i=0; i< N; i++)        {            arr[i] = scan.nextInt();        }         PriorityQueue pq = new PriorityQueue<>(Comparator.reverseOrder());        for(int i = 0; i <= N-K; i++)        {            int sum = 0;            int temp = i;            for(int j = i; j < arr.length && j < temp+K; j++)            {                sum += arr[j];            }            pq.add(sum);        }           int z = 0;           while(z < M)           {               System.out.print(pq.peek()+" ");               pq.poll();               z++;           }           System.out.println();    }    }}
-1
deytulsi183 months ago
[C++] [Time Complexity: O(nlogn)] [Space Complexity: O(n)]
 
/*
 * author: deytulsi18
 * problem: https://practice.geeksforgeeks.org/problems/k-subarray-sum/0/
 * time complexity: O(nlogn)
 * space complexity: O(n)
 * language: cpp
 */
#define deb(x) cout << #x << " = " << x << endl;

#include<bits/stdc++.h>
using namespace std;

void solution()
{
    int n, k, m;
    int sum = 0;
    
    cin >> n >> k >> m;
    
    vector<int> arr(n);
    priority_queue<int> res;
    
    for (int i = 0; i < n; i++)  cin >> arr[i];
    for (int i = 0; i < k; i++)  sum += arr[i];
    
    res.push(sum);
    
    for (int i = k; i < n; i++)
    {
        sum = sum - arr[i - k] + arr[i];
        res.push(sum);
    }
    
    for (int i = 1; i <= m; i++)
    {
        cout << res.top() << " ";
        res.pop();
    }
        
    cout << endl;
}

int main()
 {
	int testCases = 0;
	cin >> testCases;
	
	while (testCases-->0)
	    solution();
	return 0;
}
+3
doramohitkumar4 months ago
I had written the code in python
But it said time limit exceeded 😅😅
+1
badgujarsachin835 months ago
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main()
 {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n,k,m;
	    cin>>n>>k>>m;
	    int arr[n];
	    int sum=0;
	    for(int i=0;i<n;i++){
	        cin>>arr[i];
	    }
	    priority_queue<int,vector<int> ,greater<int>> q;
	    for(int i=0;i<k;i++){
	        sum+=arr[i];
	    }
	    int i=0,j=k-1;
	    while(j<n){
	        q.push(sum);
	        if(q.size()>m){
	            q.pop();
	        }
	        sum-=arr[i++];
	        if(j+1<n){
	            sum+=arr[j+1];
	        }
	        j++;
	    }
	    stack<int> st;
	    while(!q.empty()){
	        st.push(q.top());
	        q.pop();
	    }
	    while(!st.empty()){
	        cout<<st.top()<<" ";
	        st.pop();
	    }
	    cout<<endl;
	}
	return 0;
}
-1
imranwahid6 months ago
Easy C++ solution
#include<bits/stdc++.h>
using namespace std;
void helper(vector<int>&v,int k,int m)
{
    priority_queue<int,vector<int>,greater<int>>q;
    int sum=0,i=0,j=k-1;
    for(int i=0;i<k;i++)
    {
        sum+=v[i];
    }
    while(j<v.size())
    {
        q.push(sum);
        if(q.size()>m)
        {
            q.pop();
        }
        sum-=v[i];
        if(j+1<v.size())
        {
            sum+=v[j+1];
        }
        i++;
        j++;
    }
    stack<int>stk;
    while(!q.empty())
    {
        stk.push(q.top());
        q.pop();
    }
    while(!stk.empty())
    {
        cout<<stk.top()<<" ";
        stk.pop();
    }
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n,k,m;
	    cin>>n>>k>>m;
	    vector<int>v(n);
	    for(int i=0;i<n;i++)
	    {
	        cin>>v[i];
	    }
	    helper(v,k,m);
	    cout<<"\n";
	}
	return 0;
}
0
swayam60kumar7 months ago
Use the sliding window method to find the sum of each interval and priorty queue to arrange the sum in the particular order
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.