Arrays · Coding problem

Find the Minimum and Maximum

A free coding problem from the Array Traversal Basics skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Given an array of integers nums, return the minimum and maximum values in the array as [min, max].

Example

Input:  nums = [3, 1, 4, 1, 5]
Output: [1, 5]

Do it in a single pass — don't call a library min/max function twice (that's two passes) and don't sort (that's O(n log n) when O(n) is available).

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Array Traversal Basics

Solve it Find the Minimum and Maximum free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime