maximum difference b/w array n elements code example

Example 1: maximum difference in array java

public class maxDifference {

public static void main(String[] args) {
 
 int[] a = {1,4,8,15,17};
 
 int diff = 0;
 
 for(int i = 0; i < a.length-1 ; i++){
 
    if(a[i+1] - a[i] > diff)
   {
       diff = a[i+1] - a[i];
       
       }    
          }
 
 System.out.println(diff);

Example 2: maximum difference in array

public class maxDifference {

public static void main(String[] args) {
 
 int[] a = {1,4,8,15,17};
 
 int diff = 0;
 
 for(int i = 0; i < a.length-1 ; i++){
 
    if(a[i+1] - a[i] > diff)
   {
       diff = a[i+1] - a[i];
       
       }    
          }
 
 System.out.println(diff);

Tags:

Java Example