Java Program to Find Minimum and Maximum number in an Array

Leave a Comment
Write a java program to find minimum and maximum value in an array. Given an unsorted array, we have to write a code to find minimum and maximum value in an array.

We have unsorted array. So if we sort an array then the value at index 0 is minimum value and value at index n - 1 is maximum value where n is the size of an array.

We can also solve this problem in a single traversal of an array. Let's write a code to solve this problem.

Java programs

Find second largest number in array

Find Min and Max in Array

METHOD 1 : Using single traversal

package minmax;

import java.util.*;

public class MinMax {

     * @param args the command line arguments
    public static void main(String[] args) {

        int size, i, min, max;
        Scanner in = new Scanner(;
        System.out.println("Enter the size of an array");
        size = in.nextInt();
        /* Declare an array */
        int[] arr = new int[size];
        System.out.println("Enter value in an array");
        for (i = 0; i < size; i++) {
            arr[i] = in.nextInt();
        /* Intialize min and max with first value of an array */
        min = arr[0];
        max = arr[0];
        /* Find min and max in an array */
        for (i = 0; i < size; i++) {
            if ( arr[i] > max) {
                max = arr[i];
            if ( arr[i] < min) {
                min = arr[i];
        System.out.println("Max and Min value in an array is 
                            "+ min + "   "+max);

Output -

Enter the size of an array

Enter value in an array

Max and Min value in an array is   -1        9

METHOD 2: Using sorting

We have solved this problem by traversing an array. You can also solved this problem by sorting an array.

To sort an array you can use any sorting algorithm such as -

Insertion sort implementation in java

Bubble sort

Quick sort

Have a look at the time complexity of sorting algorithms .

After sorting, element at index 0 is the minimum value of an array. Element at index n-1 is the maximum value of an array.

If You Enjoyed This, Take 5 Seconds To Share It


Post a Comment