Skip to content

Instantly share code, notes, and snippets.

@epzee
Created October 23, 2017 13:27
Show Gist options
  • Save epzee/9f3c622243b8fb418a6efd1cfffaf3fe to your computer and use it in GitHub Desktop.
Save epzee/9f3c622243b8fb418a6efd1cfffaf3fe to your computer and use it in GitHub Desktop.
function maxDifference(a) {
let maxDiff = -1;
let min = a[0];
for (let i = 1; i < a.length; i++) {
if (a[i] - min > maxDiff) {
// update diff if greater diff was found
maxDiff = a[i] - min;
}
if (a[i] < min) {
// update min if smaller int was found
min = a[i];
}
}
return maxDiff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment