
Finding Minimum and Maximum in an Array using Python
Problem Statement Given an array arr[], the task is to find the minimum and maximum elements present in the array. Examples Input: arr = [1, 4, 3, 5, 8, 6] Output: [1, 8] Input: arr = [12, 3, 15, 7, 9] Output: [3, 15] Objective • Identify the smallest element • Identify the largest element • Return both values as output Approach 1: Using Loop (Manual Method) This approach involves: • Initializing two variables: o minimum → first element o maximum → first element • Iterating through the array • Updating values when a smaller or larger element is found Algorithm Start with the first element as both min and max Traverse the array For each element: o If it is smaller than current min → update min o If it is larger than current max → update max Return both values ________________________________________ Python Code arr = [1, 4, 3, 5, 8, 6] minimum = arr[0] maximum = arr[0] for i in arr: if i < minimum: minimum = i if i > maximum: maximum = i print("Minimum:", minimum) print("Maximum:", maxi
Continue reading on Dev.to Tutorial
Opens in a new tab

