Menu

Sign in to track your progress and unlock all features.

Theme style

Start learning freeLog in
01. Compute array sumE02. Find the maximum and minimum in an arrayE03. Count occurrences of a valueE

Problem No 2

Find the maximum and minimum in an array

Easy

7 minute session

Summary

Return both the minimum and maximum values from an array.

Problem statement

Given a list of comparable values (integers), return a tuple (minimum, maximum). If the list is empty, return (None, None). You should traverse the list only once (O(n) time) and O(1) extra space.

Task

Implement a function that scans an array once to determine both min and max values, handling single-element and empty arrays explicitly.

Examples

Basic example

Input

[3, 1, 4, 2]

Output

(1, 4)

Explanation

Minimum is 1 and maximum is 4.

Input format

A single argument: a list of integers, e.g. [3, 1, 4].

Output format

A tuple (min_value, max_value). For an empty list return (None, None).

Constraints

List length 0..10^6. Values are integers. Time O(n), space O(1).

Samples

Sample input 0

[7]

Sample output 0

(7, 7)

Explanation 0

Single element: both min and max are 7.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.