Codility Task:
CyclicRotation
Problem Definition:
Rotate an array to the right by a given number of steps.
Solution:
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
public int[] solution(int[] array, int k) { | |
int arrayLength = array.length; | |
int[] result = new int[arrayLength]; | |
for (int i = 0; i < array.length; i++) { | |
result[(i + k) % arrayLength] = array[i]; | |
} | |
return result; | |
} |
0 comments:
Post a Comment