Google Add

Search

Find Maximum Difference between Two Elements of an Array - Java Program

Write a program to find maximum difference between two elements of an array. Given an unsorted array find maximum difference between two element of an array.

How to find maximum difference between two elements of an array

Maximum difference  =  Maximum value of an array - Minimum value of an array

Java programs

Find second largest number in array

So to solve this problem we need to find maximum and minimum value of an array. Then subtracting these two values. we can find the maximum difference.

Find Maximum Difference between Two Elements of an Array


METHOD 1 : Using two for loops

package maximumdifference;

import java.util.*;

public class MaximumDifference {

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

    
        int size, i, j, max = 0;
        
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter the size of an array");
        
        size = in.nextInt();
        
        /*  Declare an array */
        
        int[] arr = new int[size];
        
        System.out.println("Enter values in an array");
        
        for (i = 0; i < size; i++) {
            
            arr[i] = in.nextInt();
            
        }
        
        for (i = 0; i < size; i++) {
            
            for (j = 0; j < size; j++) {
                
                if ( (arr[i] - arr[j]) > max) {
                    
                    max = arr[i] - arr[j];
                    
                }
            }
             
        }
        
        System.out.println(" Max difference is "+ max);
    
    }
    
}


Output


Enter the size of an array   5

Enter values in an array
3
1
6
7
9

Max difference is   8


METHOD 2 -  By finding minimum and maximum value in an array


package maximumdifference;

import java.util.*;

public class MaximumDifference {

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

    
        int size, i, j, max, min;
        
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter the size of an array");
        
        size = in.nextInt();
        
        /*  Declare an array */
        
        int[] arr = new int[size];
        
        System.out.println("Enter values in an array");
        
        for (i = 0; i < size; i++) {
            
            arr[i] = in.nextInt();
            
        }
        
        /* Initialize min and max value */
        
        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];
                
            }
            
        }
        
        /* Maximum difference is max - min */
        
        System.out.println(" Max difference is "+ (max - min));
    
    }
    
}




You can also use sorting algorithm to find minimum and maximum value in an array.

Insertion sort implementation in java

Bubble sort

Quick sort

time complexity of sorting algorithms .

No comments:

Post a Comment