Stack operations
Tag(s):

## Arrays, Basic Programming, Basics of Stacks, Data Structures, Easy, Implementation, Stacks, easy, stack

Problem
Editorial
Analytics

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}$
SAMPLE INPUT
6 4
1 2 4 3 3 5
SAMPLE OUTPUT
4

Explanation

In 3 operations, we remove 1,2,4 and in the fourth operation, we push 4 back into the stack. Hence, 4 is the answer.

Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in Challenge Name

Pramata Software Developer Hiring Challenge

OTHER PROBLEMS OF THIS CHALLENGE
• Basic Programming > Implementation