An array of integers is given, find the smallest difference between two elements such that the element of greater value comes before the lesser value element javascript 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);