Given an array of numbers, find the minimum and the maximum element of the array using the minimum number of comparisons.
Set the smallest number (min) to the largest possible value (infinity). Set the largest number (max) to the smallest possible value (negative infinity). Go through each number in the list.
Return: After checking all numbers in the list, return the values of min and max.
ls = [1, 2, 3, 4, 5, 6, 7, 8, 9] mn = float('inf') mx = float('-inf') for item in ls: mn = min(mn, item) mx = max(mx, item) print((mn, mx))
const list = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let min = Infinity; let max = -Infinity; for (let item of list) { min = Math.min(min, item); max = Math.max(max, item); } console.log({ min: min, max: max });