Skip to content

Instantly share code, notes, and snippets.

@amuradyan
Created December 12, 2020 11:49
Show Gist options
  • Save amuradyan/a8cce9a895b68eb552006b8235acbc16 to your computer and use it in GitHub Desktop.
Save amuradyan/a8cce9a895b68eb552006b8235acbc16 to your computer and use it in GitHub Desktop.
Hackerrank `The Birthday Bar` problem
// https://www.hackerrank.com/challenges/the-birthday-bar/problem
import scala.io._
import scala.annotation.tailrec
object Solution {
// Complete the birthday function below.
def birthday(s: Array[Int], d: Int, m: Int): Int = {
@tailrec
def loop(xs: List[Int], acc: Int = 0): Int = xs match {
case h::t if xs.length >= m => {
if (xs.take(m).sum == d) loop(t, acc + 1)
else loop(t, acc)
}
case _ => acc
}
loop(s.toList)
}
def main(args: Array[String]): Unit = {
val n = StdIn.readLine.trim.toInt
val s = StdIn.readLine.replaceAll("\\s+$", "").split(" ").map(_.trim.toInt)
val dm = StdIn.readLine.replaceAll("\\s+$", "").split(" ")
val d = dm(0).toInt
val m = dm(1).toInt
val result = birthday(s, d, m)
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment