Skip to content

Instantly share code, notes, and snippets.

@sshark
Last active October 23, 2021 15:06
Show Gist options
  • Save sshark/d603246ca4e40a36e3cff19c0815f1f3 to your computer and use it in GitHub Desktop.
Save sshark/d603246ca4e40a36e3cff19c0815f1f3 to your computer and use it in GitHub Desktop.
Max difference between increasing elemets
import Math.*
def maxDiff(xs: List[Int]): Int =
xs.scanLeft(Int.MaxValue)(min)
.tail
.zip(xs)
.map(x => x._2 - x._1)
.filter(_ > 0)
.fold(-1)(max)
maxDiff(List(7, 1, 5, 4)) // 4
maxDiff(List(8, 5, 3 ,3)) // -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment