Find Maximum and Minimum

Easy~10 min

Given a non-empty array of integers nums, return the maximum and minimum values as a two-element array [max, min].

For example, given [3, 1, 4, 1, 5], the maximum is 5 and the minimum is 1, so return [5, 1].

Examples

Example 1
Input: nums = [3, 1, 4, 1, 5]
Output: [5, 1]
Explanation: 5 is the largest value and 1 is the smallest.
Example 2
Input: nums = [42]
Output: [42, 42]
Explanation: With a single element, it is both the max and the min.
Example 3
Input: nums = [-10, -20, -3]
Output: [-3, -20]
Explanation: -3 is the largest and -20 is the smallest.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run