U

11. Find Minimum and Maximum of Array

Easy

Problem Description

Given an array of NN integers, find and print the minimum and maximum element in the array.

Input Format

  • •Line 1: An integer NN (1≤N≤1051 \le N \le 10^5), the size of the array.
  • •Line 2: NN space-separated integers A1,A2,…,ANA_1, A_2, \dots, A_N.

Output Format

Print two space-separated integers: the minimum value followed by the maximum value.

Example 1:
Input:
5
3 1 9 7 2
Output:
1 9
Explanation: The smallest value is 1, and the largest is 9.
Example 2:
Input:
1
42
Output:
42 42
Explanation: With a single element, both min and max are 42.
Example 3:
Input:
4
-10 -5 -20 -1
Output:
-20 -1
Explanation: Among negative numbers, -20 is the minimum and -1 is the maximum.
Constraints:
  • 1 <= N <= 10^5
  • -10^9 <= A[i] <= 10^9
  • Time complexity target: O(N)
  • Auxiliary space target: O(1)
Follow up:Can you find min and max using strictly <= 3N/2 comparisons?
1,180 Online