Arrays: Left Rotation

  • + 0 comments

    I used the System.arraycopy() method which was used in the video tutorial. I'm wondering if this solution is more efficient or mine?

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int k = in.nextInt();
            int a[] = new int[n];
            for(int a_i=0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            a = leftRotation(n, k, a);
            for (int i=0; i<a.length; i++) {
                System.out.print(a[i]+" ");
            }
        }
        public static int[] leftRotation(int n, int k, int[] a){
            int[] copy = new int[n];
            System.arraycopy(a, k, copy, 0, (n - k));
            System.arraycopy(a, 0, copy, (n - k), (n - (n - k)));
            return copy;
        }
    }