20220507 Rotatearray


title: "Codility RotateArray"

date: "2022-05-07"

// you can also use imports, for example: // import java.util.*;

// you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message");

class Solution {
public int[] solution(int[] A, int K) {
//check special cases
if(K == 0 ) return A;
if(A == null || A.length == 0 ) return A;
if(A.length == K) return A;
int length = A.length;
int[] resultList = new int[length];
int rightStep = 0; //the number of steps to the right
if(length > K) rightStep = K;
else rightStep = K % length;
for(int i = 0; i < length; ++i)
{
int toPosition = (i+rightStep)%length;
resultList[toPosition] = A[i];
}
return resultList;
}
}