Skip to content

Instantly share code, notes, and snippets.

@NicolaBernini
Created August 2, 2020 09:13
Show Gist options
  • Save NicolaBernini/c0c864890930573f4e3ac777c3a17a0f to your computer and use it in GitHub Desktop.
Save NicolaBernini/c0c864890930573f4e3ac777c3a17a0f to your computer and use it in GitHub Desktop.
Solution to the Hackerrank - Arrays: Left Rotation
@NicolaBernini
Copy link
Author

Scala

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 rotLeft function below.
    def rotLeft(a: Array[Int], d: Int): Array[Int] = {
    // Just using Array Slicing and Concatenation 
    a.slice(d, a.length) ++ a.slice(0, d)

    }

    def main(args: Array[String]) {
        val stdin = scala.io.StdIn

        val printWriter = new PrintWriter(sys.env("OUTPUT_PATH"))

        val nd = stdin.readLine.split(" ")

        val n = nd(0).trim.toInt

        val d = nd(1).trim.toInt

        val a = stdin.readLine.split(" ").map(_.trim.toInt)
        val result = rotLeft(a, d)

        printWriter.println(result.mkString(" "))

        printWriter.close()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment