Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Created February 27, 2022 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guilherme-HRamos/96781fdf76cafe1095a422c419613ad7 to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/96781fdf76cafe1095a422c419613ad7 to your computer and use it in GitHub Desktop.
Array Left Rotation
private fun execute(inputArray: Array<Int>, inputRotation: Int): Array<Int> {
// left rotation is basically the number of rotations
// divided by array size. The rest of this division is
// the position where first index will be placed after
// rotation.
val output = arrayListOf<Int>()
val rest = inputRotation%inputArray.size
// filling from rest until the end
for (index in rest until inputArray.size) {
val inputArrayValue = inputArray[index]
output.add(inputArrayValue)
}
// filling from 0 to rest index
for (index in 0 until rest) {
val inputArrayValue = inputArray[index]
output.add(inputArrayValue)
}
return output.toTypedArray()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment