


Try solving this problem in O(N) time complexity.
The first line of input contains the integer 'N' representing the size of the array.
The second line of input contains N space-separated integers representing the array elements.
The only output line contains the fourth-largest element if present, otherwise print -2147483648
You are not required to explicitly print the output, it has already been taken care of. Just implement the function.
1 <= N < 10^6
-10^6 <= element <= 10^6
Time Limit: 1 sec
We can find the 4th largest element by simply sorting the list, and returning the 4th element from the end.
Algorithm:
For optimizing our initial approach and avoid sorting all the elements, we can maintain a min-heap of size 4, and at each index store the 4 largest elements that occurred before the current index. In the end, we would need to return the element at the top of the heap which is the 4th largest element.
Algorithm: