Stack operations
Practice
3.3 (78 votes)
Arrays
Basic programming
Data structures
Easy
Implementation
Stacks
Easy
Problem
78% Success 38801 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given a stack of N integers. In one operation, you can either pop an element from the stack or push any popped element into the stack. You need to maximize the top element of the stack after performing exactly K operations. If the stack becomes empty after performing K operations and there is no other way for the stack to be non-empty, print -1.
Input format :
- The first line of input consists of two space-separated integers N and K.
- The second line of input consists N space-separated integers denoting the elements of the stack. The first element represents the top of the stack and the last element represents the bottom of the stack.
Output format :
- Print the maximum possible top element of the stack after performing exactly K operations.
Constraints :
- \(1 \le N \le 2*10^{6}\)
- \(1 \le A_{i} \le 10^{18}\)
- \(1 \le K \le 10^{9}\)
Submissions
Please login to view your submissions
Similar Problems
Points:20
63 votes
Tags:
ArraysData StructuresEasyImplementationQueueStacks
Points:20
53 votes
Tags:
Data StructuresDynamic programmingEasyStacks
Points:20
10 votes
Tags:
StacksBasics of StacksSetsData Structures
Editorial