Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 24, 2018 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/1fdd98c105f5a4ce03d631dd9e3f299f to your computer and use it in GitHub Desktop.
Save thmain/1fdd98c105f5a4ce03d631dd9e3f299f to your computer and use it in GitHub Desktop.
public class LargestGapBetter {
static void findGap(int [] a){
if(a==null || a.length==0){
System.out.println("Array is null, no solution found");
}
int max_so_far=a[0];
int min_so_far=a[0];
int max_diff = 0;
for (int i = 1; i <a.length ; i++) {
if(a[i]>max_so_far)
max_so_far = a[i];
if(a[i]<min_so_far)
min_so_far = a[i];
}
max_diff = max_so_far-min_so_far;
System.out.println("Largest Gap between any two elements is: " + max_diff);
}
public static void main(String[] args) {
int [] a = {2, 8, 1, 6, 10, 4};
findGap(a);
int [] b = {10, 30, 5, 16, 19};
findGap(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment