algeek
BAN USER- 2of 2 votes
AnswersHow will you serialize the binary tree ?
- algeek in India| Report Duplicate | Flag | PURGE
Amazon SDE-3 Algorithm
public class ArrayRotation
{
public static void main(String[] args){
int[] array = new int[] {1,2,3,4,5,6,7,8,9,10};
System.out.println("*********************Right Rotation*********************");
System.out.println("");
printRightRotatedArray(array,2);
System.out.println("*********************Left Rotation*********************");
System.out.println("");
printLeftRotatedArray(array,2);
}
public static void printRightRotatedArray(int[] array, int rotation){
int rotatedIndex = 0;
int n = array.length;
for(int i = 0; i < array.length; i++) {
rotatedIndex = (i + rotation)%n;
System.out.println(array[rotatedIndex]);
}
}
public static void printLeftRotatedArray(int[] array, int rotation){
int rotatedIndex = 0;
int n = array.length;
for(int i = 0; i < array.length; i++) {
rotatedIndex = (i + (n - (rotation % n)))%n;
/* if(rotatedIndex >= array.length){
rotatedIndex = rotatedIndex % array.length;
} */
System.out.println(array[rotatedIndex]);
}
}
}
Repmendezleah216, Analyst at Bosch
Hello,everybody,I'm Leah Mendez.I want to make some friends here.I’m a woman with ambition and ...
The code for this is as below,
- algeek June 12, 2015