Tuesday, July 4, 2017

CyclicRotation

import java.util.Arrays;
import java.util.stream.Collectors;

public class CyclicRotation {

     public static void main(String[] args) {
          print(solution(3, 1, 2, 3, 4, 5));
     }

     public static int[] solution(int k, int... a) {
          int l = a.length;
          int[] r = new int[l];
          for (int i = 0; i < l; i++) {
               r[(i + k) % l] = a[i];
          }
          return r;
     }

     public static void print(int... a) {
          System.out.println("[" + Arrays.stream(a).boxed().map(x -> x.toString()).collect(Collectors.joining(",")) + "]");
     }
}


No comments:

Post a Comment