Sort by

recency

|

31 Discussions

|

  • + 0 comments

    Is it good for apps like https://sosoapkapp.com/sosomod-car-parking-multiplayer/ ?

  • + 0 comments

    F# Pipeline Solution

    for T in 1..(stdin.ReadLine() |> int) do
        let K = stdin.ReadLine().Split()|>Array.map int |> fun a -> a.[1]
        stdin.ReadLine().Split()
         |> Array.countBy id
         |> Array.filter (fun (_,count) -> count >= K)
            |> function
                | [||] -> printfn "-1"
                | lst -> lst 
                            |> Array.map fst 
                            |> String.concat " " 
                            |> printfn "%s"
            
    
  • + 0 comments

    Here is a haskell solution...

    {-# LANGUAGE LambdaCase #-}
    
    import Data.List
    
    count e [] = 0
    count e (x : xs) = if x == e then 1 + count e xs else count e xs
    
    byCount c l = nub $ filter (\e -> count e l >= c) l
    
    readAllInts :: String -> [[Int]]
    readAllInts = map (map read . words) . lines
    
    showResults :: Show a => [[a]] -> String
    showResults = unlines . map (unwords . map show)
    
    takes c [] = []
    takes c l = take c l : takes c (drop c l)
    
    solve s = showResults $ map (\l -> if null l then [-1] else l) solutions
      where
        inputs = takes 2 . tail . readAllInts $ s
        solutions = map (\case [[_, c], l] -> byCount c l; _ -> []) inputs
        
    main = interact solve
    
  • + 0 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
            })
        }
    }
    
  • + 0 comments

    Elixir solution (even though it feels not very pretty...):

    defmodule Solution do
        def split_numbers(s) do
            String.split(s, " ")
            |> Enum.map(fn x -> Integer.parse(x) |> elem(0) end)
        end
        
        def find_by_k(list, k) do
            grouped = Enum.group_by(list, fn x -> x end)
            output = Enum.filter(list, fn x -> Enum.count(grouped[x]) >= k end)
            |> Enum.uniq
            if Enum.count(output) == 0 do
                [-1]
            else
                output
            end
        end
    end
    
    
    
    input = IO.read(:stdio, :all)
      |> String.split("\n")
      
    [_ | elements] = input
    Enum.chunk_every(elements, 2)
    |> Enum.map(fn [config, list] -> 
        [_, k] = Solution.split_numbers(config)
        l = Solution.split_numbers(list)
        Solution.find_by_k(l, k)
    end)
    |> Enum.map(fn line ->
        IO.puts Enum.join(line, " ")
    end)