/** Java Array This method takes in a reference to an integer array and a positi
ID: 3771220 • Letter: #
Question
/**
Java Array
This method takes in a reference to an integer array and a
positive integer n and it "rotates" the contents of the input
array to the right n places. So each element a[i] of the input
array should be moved to location a[i+n]. If the index i+n is
past the end of the array, then that index should "wrap" back
to the beginning of the array.
For example, if the input array is a = {1, 2, 3, 4, 5}, then
rotateRight(a, 2) should modify a to contain {4, 5, 1, 2, 3}.
Notice that this is a void method since it does not return
anything. The job of this method is to modify the input array.
*/
public static void rotateRight(int[] a, int n)
{
int[] temp = new int[a.length];
}
Complete this method
Explanation / Answer
public static int[] rotateLeft(int[] a, int n)
{
int[] temp = new int[a.length];
for(int i=0;i<(a.length-n);i++)
temp[i]=a[a.length + n];
for(int i=a.length-n;i<=a.length;i++)
temp[i]=a[i+n-a.length];
return temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.