Arrays: Left Rotation

  • + 2 comments

    mine is also a brute force approach but it worked check it out if it helps you

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static int[] arrayLeftRotation(int[] a, int n, int k) {
           int temp,i,j;
            for(i=0;i<k;i++){
                temp=a[0];
                for(j=1;j<n;j++){
                   a[j-1]=a[j]; 
                }
                a[n-1]=temp;
            }
          
            return a;
        }
        
        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();
            }
          
            int[] output = new int[n];
            output = arrayLeftRotation(a, n, k);
            for(int i = 0; i < n; i++)
                System.out.print(output[i] + " ");
          
            System.out.println();
          
        }
    }