javaarraysrotationleft-to-right

Java rotation of array.


I'm very new to java please check the below code. Please tell me where am wrong

Thanks in advance

import java.io.*;

import java.util.*;

public class Solution {

public void rotate(int[] nums, int k) {
    k %= nums.length;
    reverse(nums, 0, nums.length - 1);
    reverse(nums, 0, k - 1);
    reverse(nums, k, nums.length - 1);
}
 public void reverse(int[] nums, int start, int end) {
    while (start < end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }
     for(int j = 0 ; j < nums.length - 1 ; j++ )
    {
        System.out.print(nums[j] + " ");
    }

 }

}

public static void main(String[] args) {



    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int n = in.nextInt();
    int[] nums = new int[size];


   for(int i = 0; i<nums.lenght-1;i++)
   {
       nums[i] = scan.nextInt();
   }

 rotate(nums, n);

}

}


Solution

  • OK, here is a huge list of problems with the code and following are their solutions :

    Fix all this and i think your code will work just fine.