Skip to main content

Maximum Element Hackerrank Solution Java | Hackerrank Data Structure

 Maximum Element Hackerrank Solution Java

For Explanation Video:


Sample Input

STDIN   Function
-----   --------
10      operations[] size n = 10
1 97    operations = ['1 97', '2', '1 20', ....]
2
1 20
2
1 26
1 20
2
3
1 91
3

Sample Output

26
91

Code:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
    public static void main(String[] args){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        Stack<Integer> st = new Stack<Integer>();
        PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());
        while(n-->0){
            int t = scn.nextInt();
            switch(t){
                case 1:
                    int num = scn.nextInt();
                    st.push(num);
                    heap.add(num);
                    break;
                case 2:
                    int num1 = st.pop();
                    heap.remove(num1);
                    break;
                case 3:
                    System.out.println(heap.peek());
                    break;
            }
        }
    }
}

Comments