Created
February 27, 2022 03:16
-
-
Save Guilherme-HRamos/96781fdf76cafe1095a422c419613ad7 to your computer and use it in GitHub Desktop.
Array Left Rotation
This file contains 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
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