Given an input integer array arr, the arr needs to be rotated rightwards by n places. Note that n is a positive number and can be greater than the size of input array also.
| Input Parameters |
Expected outputs |
arr={1,2,3,4,5,6,7,8}
n=3
|
6,7,8,1,2,3,4,5
|
arr={1,2,3,4,5,6,7,8}
n=10
|
7,8,1,2,3,4,5,6
|
arr={1,2,3,4,5,6,7,8}
n=1
|
8,1,2,3,4,5,6,7
|
arr={1,2,3,4,5,6,7,8}
n=8
|
1,2,3,4,5,6,7,8
|
| The java method should accept following input parameters: arr (int array), rotCnt (count of position rotation to be done).
|
| rotCnt can be greater than arr.length, so we need to handle this special case.
|
| When rotCnt=arr.length, the array elements return to its original position, so no rotation needs to be done. When (rotCnt > arr.length) we can calculate actual moves to be done by this formula: moveCnt= rotCnt % arr.length. We need to move arr elements only moveCnt places in this case.
|
| To rotate arr by moveCnt places first we need to move the last moveCnt element of arr to a temporary array tempArray. This can be done by a for loop.
|
| After above step, in array arr, move value at each index forward by moveCnt places. This can be done using a for loop.
|
| Next, move moveCnt elements from tempArray to start of arr.
|
| Now rotation by moveCnt places is complete and we can return the rotated array.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayRotateByNPlaces {
public int[] arrayRotateByNPlaces(int[] arr, int rotCnt) throws Exception{
int moveCnt= rotCnt % arr.length;
int[] tempArray=new int[moveCnt];
int tempArrayIndex=moveCnt-1;
for (int idx=arr.length-1; idx>arr.length-1-moveCnt; idx--) {
tempArray[tempArrayIndex]=arr[idx];
tempArrayIndex--;
}
for (int idx=arr.length-1; idx>moveCnt-1; idx--) {
arr[idx]=arr[idx-moveCnt];
}
for (int idx=0; idx<=tempArray.length-1; idx++) {
arr[idx]=tempArray[idx];
}
return arr;
}
public static void main(String[] args) {
ArrayRotateByNPlaces ap=new ArrayRotateByNPlaces();
int[] arr;
try {
int[] intArray1 = {1,2,3,4,5,6,7,8};
printArraySummary(intArray1, "Original Array:");
arr=ap.arrayRotateByNPlaces(intArray1, 3);
printArraySummary(arr, "Array rotated by "+3+" places : ");
int[] intArray2 = {1,2,3,4,5,6,7,8};
printArraySummary(intArray2, "Original Array:");
arr=ap.arrayRotateByNPlaces(intArray2, 10);
printArraySummary(arr, "Array rotated by "+10+" places : ");
int[] intArray3 = {1,2,3,4,5,6,7,8};
printArraySummary(intArray3, "Original Array:");
arr=ap.arrayRotateByNPlaces(intArray3, 1);
printArraySummary(arr, "Array rotated by "+1+" places : ");
int[] intArray4 = {1,2,3,4,5,6,7,8};
printArraySummary(intArray4, "Original Array:");
arr=ap.arrayRotateByNPlaces(intArray4, 8);
printArraySummary(arr, "Array rotated by "+8+" places : ");
int[] intArray5 = {1};
printArraySummary(intArray5, "Original Array:");
arr=ap.arrayRotateByNPlaces(intArray5, 1);
printArraySummary(arr, "Array rotated by "+1+" places : ");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}