Header Ad

HackerEarth Stack and Queue Nissan problem solution

In this HackerEarth Stack and Queue Nissan problem solution, You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert the stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove exactly K elements such that the sum of the K removed elements is maximized.


HackerEarth Stack and Queue <Nissan> problem solution


HackerEarth Stack and Queue Nissan problem solution.

#include <bits/stdc++.h>
#define ll long long
#define MAX 2000000
using namespace std;

int main() {
ll n,k;
cin>>n>>k;
ll arr[n+1];
for(int i=0;i<n;i++) cin>>arr[i];
ll prefix[n+1];
prefix[0]=arr[0];
for(int i=1;i<n;i++) prefix[i]=prefix[i-1]+arr[i];
ll ans=0;
for(int i=0;i<k;i++){
ll temp=prefix[i]+prefix[n-1]-prefix[n-k+i];
ans=max(ans,temp);
}
cout<<ans;

return 0;
}


Post a Comment

0 Comments