Skip to content

Instantly share code, notes, and snippets.

@stsatlantis
Created September 25, 2016 22:54
Show Gist options
  • Save stsatlantis/95699c4324bacb550c036da99ddf57b3 to your computer and use it in GitHub Desktop.
Save stsatlantis/95699c4324bacb550c036da99ddf57b3 to your computer and use it in GitHub Desktop.
Given integers n, l and r, find the number of ways to represent n as a sum of two integers A and B such that l ≤ A ≤ B ≤ r.
def countSumOfTwoRepresentations2(n: Int, l: Int, r: Int): Int = {
if(l > n/2) 0 else math.min(n/2 - l, r - n/2) + (if(n%2 == 1) 0 else 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment