Back to Practice Dashboard
Python BasicsEasy
Find the Smallest and largest element in an array
Learn how to solve the 'Find the Smallest and largest element in an array' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function find_min_max(arr) that takes a list of integers arr and returns a tuple (min_val, max_val) containing both the smallest and largest elements in the array.
Constraints
- •1 <= len(arr) <= 10^5
- •-10^9 <= arr[i] <= 10^9
Examples
Example 1
Input
arr = [3, 7, 2, 8, 1]
Output
(1, 8)
Explanation
The smallest element is 1 and the largest is 8.
Example 2
Input
arr = [-5, -2, -8, -1]
Output
(-8, -1)
Explanation
Min is -8, max is -1.
Example 3
Input
arr = [42]
Output
(42, 42)
Explanation
Single element is both the min and max.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.