Arrays Left and Right Rotation in Java | HackerRank Solution for Array Left Rotation.
Example 1 :- Array Left Rotation by any given number in java (Approach 1)
import java.util.Arrays;
import java.util.Scanner;
public class ArraysLeftRotation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array");
int sizeOfArray = sc.nextInt();
System.out.println("Enter values");
int[] array = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray; i++) {
array[i] = sc.nextInt();
}
System.out.println("Enter number of left rotation");
int rotation = sc.nextInt();
// Loop until given rotation
for (int i = 0; i < rotation; i++) {
// Store first array element into temp variable
int tempVariable = array[0];
// Loop through array and shift element by 1.
for (int j = 0; j < array.length-1; j++) {
array[j] = array[j + 1];
}
// Store 0th array element in last position.
array[array.length - 1] = tempVariable;
}
System.out.println(Arrays.toString(array));
}
}
Output :-
Enter size of Array
5
Enter values
1 2 3 4 5
Enter number of left rotation
2
[3, 4, 5, 1, 2]
_______________________
Enter size of Array
10
Enter values
1 2 3 4 5 6 7 8 9 0
Enter number of left rotation
5
[6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
Example 2 :- Array Left Rotation by any given number in java (Approach 2)
import java.util.Arrays;
import java.util.Scanner;
public class ArraysLeftRotation {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array");
int sizeOfArray = sc.nextInt();
System.out.println("Enter values");
int[] array = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray; i++) {
array[i] = sc.nextInt();
}
System.out.println("Enter number of left rotation");
int rotation = sc.nextInt();
int[] newArray = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray ; i++) {
if (rotation + i < sizeOfArray) {
newArray[i] = array[rotation+i];
} else {
newArray[i] = array[(i+rotation) % sizeOfArray];
}
}
System.out.println(Arrays.toString(newArray));
}
}
Example 3 :- Array Right Rotation by any given Number
import java.util.Arrays;
import java.util.Scanner;
public class ArraysLeftRotation {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array");
int sizeOfArray = sc.nextInt();
System.out.println("Enter values");
int[] array = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray; i++) {
array[i] = sc.nextInt();
}
System.out.println("Enter number of left rotation");
int rotation = sc.nextInt();
for (int i = 0; i < rotation; i++) {
int tempVariable = array[array.length-1];
for (int j = array.length-1; j > 0; j--) {
array[j] = array[j - 1];
}
array[0] = tempVariable;
}
System.out.println(Arrays.toString(array));
}
}
Output :-
Enter size of Array
5
Enter values
1 2 3 4 5
Enter number of left rotation
2
[4, 5, 1, 2, 3]
_______________________
Enter size of Array
10
Enter values
1 2 3 4 5 6 7 8 9 0
Enter number of left rotation
4
[7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
Check out other HackerRank problem's solution in Java :-
- Two Dimension DS Problem Solution in Java
- Repeated String HackerRank Solution
- Sales by Match HackerRank Solution
- Counting Valleys HackerRank Solution
Comments
Post a Comment