Maximum difference between two elements such that larger element appears after the smaller number | Java program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize Result ; Initialize max element from right side ; Driver program to test above function ; Function calling
import java . io . * ; class GFG { static int maxDiff ( int arr [ ] , int n ) { int maxDiff = - 1 ; int maxRight = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { int diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; int n = arr . length ; System . out . println ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }