Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Stack and Queue Nissan problem solution

YASH PAL, 31 July 2024
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;
}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes