You are viewing a single comment's thread. Return to all comments →
Here's a recursive solution. It's not efficient, but since this is in recursion section, here it is:
import scala.io.StdIn.{readLine, readInt} object Solution { def checkRepetitions(array: List[Int], x: Int, k: Int): Boolean = { if (k == 0) true else if (array.isEmpty) false else if (array.head == x) checkRepetitions(array.tail, x, k - 1) else checkRepetitions(array.tail, x, k) } def main(args: Array[String]) { val testCount = readInt (1 to testCount).map(_ => { val metaList = readLine.split(" ").map(_.toInt).toList val array = readLine.split(" ").map(_.toInt).toList val filteredList = array.distinct .filter(checkRepetitions(array, _, metaList(1))) if (filteredList.isEmpty) print(-1) else filteredList.foreach(x => print(x + " ")) println }) } }
Seems like cookies are disabled on this browser, please enable them to open this website
Filter Elements
You are viewing a single comment's thread. Return to all comments →
Here's a recursive solution. It's not efficient, but since this is in recursion section, here it is: