Find Smallest and Largest Elements
EasyPractice library · +10 XPheapsmin-max
Given a list of N integers, find the smallest and the largest element in the list. You must use two heaps: one min-heap to find the smallest element and one max-heap to find the largest element. The input list may contain duplicates.
Input
The first line contains an integer N, the number of elements. The second line contains N space-separated integers, the elements of the list.
Output
Print two space-separated integers: the smallest element followed by the largest element.
Constraints
1 <= N <= 1000. The integers are between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 10 4 8 15 3
Output
3 15
The smallest element is 3 and the largest is 15.
Example 2
Input
7 5 5 5 5 5 5 5
Output
5 5
If all elements are the same, the smallest and largest are that element.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 10 4 8 15 3
Example 2 input
7 5 5 5 5 5 5 5