Didart

Array Rotation

Oct 3rd, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ArrayRotation {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.         String[] array = input.split(" ");
  9.         int countRotations = Integer.parseInt(scanner.nextLine());
  10.  
  11.         for (int rotation = 1; rotation <= countRotations; rotation++) {
  12.  
  13.             String firstElement = array[0];
  14.  
  15.             for (int index = 0; index < array.length - 1; index++) {
  16.                 array[index] = array[index + 1];
  17.             }
  18.             array[array.length - 1] = firstElement;
  19.         }
  20.         for(String element : array) {
  21.             System.out.print(element + " ");
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment