
Finding Maximum and Minimum Element in an Array
Introduction In programming, finding the maximum and minimum elements in an array is a basic and important problem. It helps us understand loops, comparisons, and problem-solving techniques. Problem Statement Given an array of integers, find: The minimum (smallest) element The maximum (largest) element Approach We can solve this problem using a simple method: Assume the first element as both minimum and maximum Traverse through the array Compare each element with current min and max Update values accordingly Python Code def find_min_max(arr): minimum = arr[0] maximum = arr[0] for num in arr: if num < minimum: minimum = num if num > maximum: maximum = num return minimum, maximum Example usage arr = [10, 5, 20, 8, 2] min_val, max_val = find_min_max(arr) print("Minimum:", min_val) print("Maximum:", max_val) ouput: Minimum : 2 Maximum : 20
Continue reading on Dev.to Python
Opens in a new tab


