Skip to content

Instantly share code, notes, and snippets.

@ienugr
Created October 25, 2017 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ienugr/3137f36d07108e4493eac0742b2d9a3f to your computer and use it in GitHub Desktop.
Save ienugr/3137f36d07108e4493eac0742b2d9a3f to your computer and use it in GitHub Desktop.
A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void LeftShift(int arr[], int n) {
int temp = arr[0];
for (int i = 0; i < n-1; i++) {
arr[i] = arr[i+1];
}
arr[n-1] = temp;
}
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();
}
/* Left rotate for k times */
for (int i = 0; i < k; i++) {
LeftShift(a, n);
}
/* Displaying the output */
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment