Skip to content

Instantly share code, notes, and snippets.

@amuradyan
Created December 9, 2020 15:49
Show Gist options
  • Save amuradyan/b37b92ab9803c9b7dbaebc7977571dfb to your computer and use it in GitHub Desktop.
Save amuradyan/b37b92ab9803c9b7dbaebc7977571dfb to your computer and use it in GitHub Desktop.
Hackerrank `Apples and oranges` problem
import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._
object Solution {
// Complete the countApplesAndOranges function below.
def countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: Array[Int], oranges: Array[Int]): Unit = {
def filterAndCount(es: Array[Int], f: Int => Boolean, z: Int): Int = es.filter(f).map(z + _).filter(c => s <= c && c <= t).length
val applesOnTheHouse = filterAndCount(apples, _ >= 0, a)
val orangesOnTheHouse = filterAndCount(oranges, _ <= 0, b)
println(s"$applesOnTheHouse\n$orangesOnTheHouse")
}
def main(args: Array[String]): Unit = {
val stdin = scala.io.StdIn
val st = stdin.readLine.split(" ")
val s = st(0).trim.toInt
val t = st(1).trim.toInt
val ab = stdin.readLine.split(" ")
val a = ab(0).trim.toInt
val b = ab(1).trim.toInt
val mn = stdin.readLine.split(" ")
val m = mn(0).trim.toInt
val n = mn(1).trim.toInt
val apples = stdin.readLine.split(" ").map(_.trim.toInt)
val oranges = stdin.readLine.split(" ").map(_.trim.toInt)
countApplesAndOranges(s, t, a, b, apples, oranges)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment