Created
October 23, 2017 13:27
-
-
Save epzee/9f3c622243b8fb418a6efd1cfffaf3fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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