Skip to content

Instantly share code, notes, and snippets.

@rootid
Created May 24, 2015 21:40
Show Gist options
  • Save rootid/348ff506c848c8b18ca3 to your computer and use it in GitHub Desktop.
Save rootid/348ff506c848c8b18ca3 to your computer and use it in GitHub Desktop.
//Find Idx difference
//Input: {34, 8, 10, 3, 2, 80, 30, 33, 1}
//Output: 78
//Input: {9, 2, 3, 4, 5, 6, 7, 8, 18, 0}
//Output:16
//Input: {1, 2, 3, 4, 5, 6}
//Output: 5
//Input: {6, 5, 4, 3, 2, 1}
//Output: 0
public static int getMaxDifference (int a[]) {
int len = a.length;
int result = 0;
if (len > 1) {
int min = a[0];
for (int i=1;i<len;i++) {
if (min > a[i]) {
min = a[i];
} else {
if (result < (a[i] - min) ) {
result = a[i] - min;
}
}
}
}
//System.out.println(result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment