Created
August 3, 2019 08:59
-
-
Save atonamy/a3800fdf89e06bf726b1a16f1a6a5601 to your computer and use it in GitHub Desktop.
This file contains 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
fun maxDifference(arr: Array<Int>): Int { | |
var min: Pair<Int, Int> = Pair(0, 0) | |
var max: Pair<Int, Int> = Pair(0, 0) | |
var result = -1 | |
for(i in 0 until arr.size) { | |
when { | |
i == 0 -> { | |
min = Pair(i, arr[i]) | |
max = Pair(i, arr[i]) | |
} | |
min.second > arr[i] -> min = Pair(i, arr[i]) | |
arr[i] > min.second -> max = Pair(i, arr[i]) | |
} | |
if(max.first > min.first && max.second > min.second) | |
result = kotlin.math.max(result, max.second - min.second) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment