Skip to main content

Array Basics

Find Minimum Element in Array

In problem-solving, identifying the least or smallest value in a dataset is a crucial skill, as it often represents the baseline, minimum requirement, or the most critical limiting factor. For instance, finding the minimum element in an array helps us determine the lowest score, minimum cost, or the least significant value in a given context. Let’s explore how to efficiently achieve this fundamental task.

Given an array of integers, we need to find the minimum element in the array. The function should print the minimum value found in the array.

Example

Input: nums = [3, 7, 11, 2, 1, 10]
Output: 1
Explanation: The minimum element in the array is 1.

Input: nums = [3, 4, 9, 5, 2, 5, 9, 10]
Output: 2
Explanation: The minimum element in the array is 2.

Intuition

Imagine you're in a queue of people, and your goal is to find the shortest person. You start by assuming that the first person you see is the shortest. As you walk down the line, you compare each person with the shortest one you've seen so far. If you find someone shorter, you update your "shortest" to that person. By the time you reach the end of the queue, the last person you identified as the shortest is indeed the shortest person in the entire line. This is exactly how the algorithm for finding the minimum element in an array works.

We will use this logic to find the minimum number in a given array.

To solve this problem, we need to traverse the entire array (just like walking along the entire queue) and keep track of the minimum element (shortest person) encountered during the traversal. We start by assuming that the first element in the array is the minimum.

Why assume the minimum as the first element (nums[0]) in the beginning?

Assuming the first element to be the minimum initially provides a reference point for comparison. As we iterate through the array, we compare each element with this assumed minimum, updating it whenever we find a smaller value. This method simplifies finding the minimum by ensuring we always have the smallest value encountered so far.
Let's say our array is nums = [3, 7, 11, 2, 1, 10]. Initially, we assume the minimum element is 3 (the first element). We then iterate through the array, starting from the second element. For each element, we compare it with the current minimum element. If the current element is smaller than the current minimum, we update the minimum element to be the current element.

Approach

  • Initialize the minimum value as the first element of the array (nums[0]). This acts as our starting point for comparison. Iterate through the array starting from the second element (i = 1).
    • For each element, compare it with the current minimum value.
    • If the current element is smaller, update the minimum value to this element.
  • After the loop finishes, the minimum value is guaranteed to hold the smallest element in the array.
  • Print the minimum value.

Dry Run

Initialization:

  • The array nums contains: [3, 7, 11, 2, 1, 10].
  • The size of the array n is 6.
  • The variable minElement is initialized to the first element of the array, minElement = nums[0] = 3.

Iteration Process:

  1. First Iteration (i = 1):
    • Current element: nums[1] = 7.
    • Compare: Is nums[1] < minElement? (7 < 3).
    • Result: No. The value of minElement remains 3.
  2. Second Iteration (i = 2):
    • Current element: nums[2] = 11.
    • Compare: Is nums[2] < minElement? (11 < 3).
    • Result: No. The value of minElement remains 3.
  3. Third Iteration (i = 3):
    • Current element: nums[3] = 2.
    • Compare: Is nums[3] < minElement? (2 < 3).
    • Result: Yes. Update minElement to 2. Now, minElement = 2.
  4. Fourth Iteration (i = 4):
    • Current element: nums[4] = 1.
    • Compare: Is nums[4] < minElement? (1 < 2).
    • Result: Yes. Update minElement to 1. Now, minElement = 1.
  5. Fifth Iteration (i = 5):
    • Current element: nums[5] = 10.
    • Compare: Is nums[5] < minElement? (10 < 1).
    • Result: No. The value of minElement remains 1.

Final Step:
After completing all iterations, the smallest element in the array is found to be minElement = 1.

Output:
The minimum element is: 1.

Code for All Languages
C++
#include <iostream>
using namespace std;

// Function to find the minimum element in the array
void findMinElement(int nums[], int n) {

    // Initialize the minimum with the first element of the array
    int minElement = nums[0];

    // Iterate through the array starting from the second element
    for (int i = 1; i < n; ++i) {
    
        // Compare the current element with the minimum element
        if (nums[i] < minElement) {
        
            // Update the minimum element if the current element is smaller
            minElement = nums[i];
        }
    }
    
    // Print the minimum element found in the array
    cout << "The minimum element is: " << minElement << endl;
}

Java
import java.util.Scanner;

public class LearnYard {
    // Function to find the minimum element in the array
    static void findMinElement(int[] nums, int n) {
        // Initialize the minimum with the first element of the array
        int minElement = nums[0];

        // Iterate through the array starting from the second element
        for (int i = 1; i < n; i++) {
            // Compare the current element with the minimum element
            if (nums[i] < minElement) {
                // Update the minimum element if the current element is smaller
                minElement = nums[i];
            }
        }

        // Print the minimum element found in the array
        System.out.println("The minimum element is: " + minElement);
    }
}

Python
def find_min_element(nums):
    # Initialize the minimum with the first element of the array
    min_element = nums[0]

    # Iterate through the array starting from the second element
    for i in range(1, len(nums)):
        # Compare the current element with the minimum element
        if nums[i] < min_element:
            # Update the minimum element if the current element is smaller
            min_element = nums[i]
    
    # Print the minimum element found in the array
    print(f"The minimum element is: {min_element}")


Javascript
function findMinElement(nums) {
    // Initialize the minimum with the first element of the array
    let minElement = nums[0];

    // Iterate through the array starting from the second element
    for (let i = 1; i < nums.length; i++) {
        // Compare the current element with the minimum element
        if (nums[i] < minElement) {
            // Update the minimum element if the current element is smaller
            minElement = nums[i];
        }
    }

    // Print the minimum element found in the array
    console.log("The minimum element is: " + minElement);
}

Time Complexity

The time complexity of this solution is linear, O(n), because we need to traverse all n elements in the array exactly once to determine the minimum.

Space Complexity

  • Total Space Complexity: The total space complexity is O(n) because the input array takes up O(n) space.
  • Auxiliary Space: The auxiliary space complexity is O(1) since we only need a single variable to keep track of the minimum element, which does not depend on the size of the array.