Last active
August 22, 2016 19:52
-
-
Save temochka/5d52dbf00acc8be1292943adb89b0e30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module OptimizedSolution where | |
import Control.Monad | |
import Data.List | |
import Data.Array | |
import Data.Char | |
readToken :: IO String | |
readToken = do | |
ch <- getChar | |
lex ch | |
where lex ch | isDigit ch = liftM (ch:) readToken | |
| isSpace ch = return "" | |
| otherwise = undefined | |
readInt :: IO Int | |
readInt = liftM read readToken | |
rotatedIndex :: Int -> Int -> Int -> Int | |
rotatedIndex n k m = (k * (pred n) + m) `mod` n | |
executeQuery :: Int -> Int -> Array Int Int -> IO () | |
executeQuery n k a = do | |
m <- liftM read getLine | |
putStrLn $ show $ a!(rotatedIndex n k m) | |
main :: IO () | |
main = do | |
n <- readInt | |
k <- readInt | |
q <- readInt | |
a <- liftM (array (0, (pred n))) $ mapM (\i -> liftM ((,) i) readInt) $ [0..(pred n)] | |
replicateM_ q (executeQuery n k a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clojure-hackerrank.core | |
(:import java.util.Scanner)) | |
(defn circular-array-rotation | |
[source] | |
(let [scanner (Scanner. source) | |
n (.nextInt scanner) | |
k (.nextInt scanner) | |
q (.nextInt scanner) | |
a (vec (repeatedly n #(.nextInt scanner)))] | |
(dotimes [_ q] | |
(let [m (.nextInt scanner)] | |
(println (nth a (mod (+ (* k (dec n)) m) n))))))) | |
(circular-array-rotation *in*) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Solution where | |
import Control.Monad | |
import Data.List | |
import Data.Array | |
type NumericArray = Array Int Int | |
parseNumbers :: String -> [Int] | |
parseNumbers str = map read $ words str | |
readArray :: Int -> String -> NumericArray | |
readArray n str = array (0, (pred n)) $ zip [0..] $ parseNumbers str | |
rotatedIndex :: Int -> Int -> Int -> Int | |
rotatedIndex n k m = (k * (pred n) + m) `mod` n | |
executeQuery :: Int -> Int -> NumericArray -> IO () | |
executeQuery n k a = do | |
m <- liftM read getLine | |
putStrLn $ show $ a!(rotatedIndex n k m) | |
main :: IO () | |
main = do | |
[n,k,q] <- liftM parseNumbers getLine | |
a <- liftM (readArray n) getLine | |
replicateM_ q (executeQuery n k a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment