Created
October 25, 2017 13:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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