
Find Minimum and Maximum in an Array
Introduction Arrays are fundamental data structures used to store multiple elements. One of the most common operations is finding the minimum (smallest) and maximum (largest) values in an array. This problem is simple but very important for building logical thinking and is frequently asked in coding interviews. Problem Statement Given an array arr[], your task is to find: The minimum element The maximum element Return both values. Examples Example 1: Input: [1, 4, 3, 5, 8, 6] Output: [1, 8] Explanation: Minimum = 1, Maximum = 8 Example 2: Input: [12, 3, 15, 7, 9] Output: [3, 15] Explanation: Minimum = 3, Maximum = 15 Intuition To find the minimum and maximum: Traverse the array once Keep track of: Smallest value seen so far Largest value seen so far Approach 1.Initialize: min_val = arr[0] max_val = arr[0] 2.Traverse the array: If element < min_val → update min_val If element > max_val → update max_val 3.Return both values Code (Python) def find_min_max ( arr ): min_val = arr [ 0 ] max_
Continue reading on Dev.to Beginners
Opens in a new tab



