import scala.collection.mutable
object Main {
def goalsFulfilled(trades: Array[Int]): Boolean = {
var frequencyMap = new mutable.HashMap[Int, Int]().withDefaultValue(0)
var imaginedMap = new mutable.HashMap[Int, Int]().withDefaultValue(0)
for (n <- trades) {
frequencyMap(n) = frequencyMap(n) + 1
}
for (n <- trades) {
var flag = false
if (frequencyMap(n) == 0)
flag = true
else if (imaginedMap(n) > 0) {
imaginedMap(n) += - 1
imaginedMap(n + 1) += 1
}
else if (frequencyMap(n + 1) > 0 && frequencyMap(n + 2) > 0) {
frequencyMap(n + 1) += -1
frequencyMap(n + 2) += -1
imaginedMap(n + 3) += 1
}
else
return false
if(!flag)
frequencyMap(n) += -1
}
true
}
def main(args: Array[String]): Unit = {
val trades = Array(1, 2, 3, 3, 4, 4, 5, 5)
if (goalsFulfilled(trades) == false)
println("The goals have not been fulfilled!")
else println("The goals have been fulfilled!")
}
}