Skip to content

Instantly share code, notes, and snippets.

@atonamy
Created August 3, 2019 08:59
Show Gist options
  • Save atonamy/a3800fdf89e06bf726b1a16f1a6a5601 to your computer and use it in GitHub Desktop.
Save atonamy/a3800fdf89e06bf726b1a16f1a6a5601 to your computer and use it in GitHub Desktop.
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