|
U
Pro11. Find Minimum and Maximum of Array
Easy
Problem Description
Given an array of N integers, find and print the minimum and maximum element in the array.
Input Format
- •Line 1: An integer N (1≤N≤105), the size of the array.
- •Line 2: N space-separated integers A1,A2,…,AN.
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^9Time 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit