703457. Min and Max in Array

Basic

Min and Max in Array

Slug: find-minimum-and-maximum-element-in-an-array4428

Difficulty: Basic

Topic Tags: Arrays, Data Structures

Company Tags: None

Summary

This problem involves finding the minimum and maximum elements in an array. The key concepts involved are arrays and basic algorithmic thinking.

Detailed Explanation

To solve this problem, we can use a simple iteration through the array to find the minimum and maximum elements. Here's a step-by-step breakdown of the solution:

  1. Initialize two variables min and max to store the minimum and maximum elements respectively.
  2. Iterate through the array from index 0 to the last element.
  3. For each element, check if it is less than or equal to the current min. If it is, update min with this new value.
  4. Similarly, for each element, check if it is greater than the current max. If it is, update max with this new value.
  5. After iterating through the entire array, the values of min and max will be the minimum and maximum elements in the array respectively.

The time complexity of this solution is O(n), where n is the size of the array, since we are iterating through the array once. The space complexity is O(1) as we are only using a constant amount of space to store the minimum and maximum elements.

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static void main(String[] args) {
        int[] arr = {12, 13, 1, 10, 34, 16};
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        for (int i : arr) {
            if (i < min) {
                min = i;
            }
            if (i > max) {
                max = i;
            }
        }

        System.out.println("Minimum element is " + min);
        System.out.println("Maximum element is " + max);
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.