Maximum and Minimum in Array
Given an array of integers, find both the maximum and the minimum value present in the array using a single pass. Return [maximum, minimum], with the maximum…
Given an array of integers, find both the maximum and the minimum value present in the array using a single pass. Return `[maximum, minimum]`, with the maximum first and the minimum second.
- `1 <= n <= 10^5` - `-10^9 <= arr[i] <= 10^9` - The array contains at least one element. - All elements are integers (positive, negative, or zero). - The interactive visualizer accepts at most 12 values; longer statement-valid arrays are execution-only.
Example 1: input arr = [6, 2, 11, 4, 8]; output [11, 2]. Scanning left to right, the running max rises to 11 (index 2) and the running min falls to 2 (index 1). No other element beats either value.
Example 2: input arr = [-3, -10, -1, -7]; output [-1, -10]. All elements are negative. -1 is the least negative (largest) and -10 is the most negative (smallest). Initialising from the first element rather than zero is essential here.
Example 3: input arr = [5]; output [5, 5]. A single-element array has one value; it is simultaneously the maximum and the minimum.
Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: linear-scan.