Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 24, 2018 00:14
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/2aba52e69e406ae7c12d7e564156c32d to your computer and use it in GitHub Desktop.
Save thmain/2aba52e69e406ae7c12d7e564156c32d to your computer and use it in GitHub Desktop.
public class LargestGapNaive {
static void findGap(int [] a){
int maximum_diff = 0;
for (int i = 0; i <a.length ; i++) {
for (int j = 1; j <a.length ; j++) {
int x = a[i];
int y = a[j];
int diff = Math.abs(x-y);
if(maximum_diff<diff)
maximum_diff = diff;
}
}
System.out.println("Largest Gap between any two elements is: " + maximum_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