Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This method takes in a reference to an integer array and a positive integer n an

ID: 3771222 • Letter: T

Question

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 class Rotation{

    public static void rotateRight(int a[],int n)
{


int k=0,i,j;
int[] temp = new int[a.length];


     while(k<n)

     {
     for(i=0;i<a.length;i++)
     {
         if(i+n<a.length)
         temp[i+n]=a[i];
         else
         break;
     }
     for(j=0;i<5;i++,j++)
     temp[j]=a[i];
     k++;
}
for(i=0;i<5;i++)
System.out.println(temp[i]);
}

     public static void main(String []args)
     {
        int a[]=new int [5];
        for(int i=0;i<5;i++)
        a[i]=i+1;
        Rotation.rotateRight(a,2);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote