Find Minimum and Maximum in an Array
My Thought Process (Java DSA) When I saw this problem on GeeksforGeeks, it looked very straightforward. But instead of rushing into coding, I tried to think about the most efficient way to solve it...

Source: DEV Community
My Thought Process (Java DSA) When I saw this problem on GeeksforGeeks, it looked very straightforward. But instead of rushing into coding, I tried to think about the most efficient way to solve it. Problem Statement We are given an array of integers arr[]. The task is to find the minimum and maximum elements in the array. Initial Thinking Letβs take an example: arr = [1, 4, 3, 5, 8, 6] From this: Minimum = 1 Maximum = 8 At first, one idea was to sort the array and pick the first and last elements. But sorting takes extra time, which is unnecessary for this problem. Key Observation We donβt need to rearrange the array. We just need to scan through it once and keep track of: The smallest value seen so far The largest value seen so far my Way Approach Assume the first element is both min and max Traverse the array from index 1 For every element: If it is smaller than min, update min If it is larger than max, update max how it Runs arr = [12, 3, 15, 7, 9] Start: min = 12, max = 12 i = 1 β