MAXSTACK - Editorial

PROBLEM LINK:

Practice
Contest

Author: rocknot
Tester: root_rvl
Editorialist:rocknot

DIFFICULTY:

EASY

PREREQUISITES:

Implementation ,queue.

PROBLEM:

Aditya is good at making posters so he participated at poster making contest at his college. His poster stood out among the rest because it was different and appealing, So he easily won the competition. Prices for this competition were unique, and as a winner he was given a chance to select K prices of his choice .So he was given list of prices, but there is one problem he can only select consecutive prices. Aditya is clever and decided to make most of it. You are given an array of N integers denoting value of each price and you need to select K consecutive prices such that the total value he gets is maximum.

EXPLANATION:

You just need to add K elements to queue and then Keep adding from K+1 element till Nth element and make sure that length of queue is equal to K and you just need to keep track of sum of the maximum value of queue.

SOLUTIONS:

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

int main(){
int T;
cin>>T;
while(T--){
    int N,K;
    cin>>N>>K;
    int arr[N];
    for(int i=0;i<N;i++){
        cin>>arr[i];
    }
    int sum=0,max=0;
    for(int i=0;i<K;i++){
        sum+=arr[i];
    }
    max=sum;
    int temp=0;
    for(int i=K;i<N;i++){
        sum-=arr[temp];
        sum+=arr[i];
        temp++;
        if(sum>max){
            max=sum;
        }
    }
    cout<<max<<endl;
}
return 0;

}