How to rotate an array from a particular index?

0 votes

Can anyone help me with the program to rotate an array from a particular index given as an input from a user.

Eg:

User input must contain length of array and position of rotation.

5, 3

1,2,3,4,5

Output:

4,5,1,2,3
Mar 4 in Java by Sunita
14 views

1 answer to this question.

0 votes

This is the code for the above said problem.

import java.util.*;

class Rotate
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T>0)
        {
             int N = in.nextInt();
             int D = in.nextInt();
             int arr[] = new int[N];
             for(int i=0;i<N;i++)
             {
                  arr[i]=in.nextInt();
             }
             int arr1[]= new int [N];
             for (int j=0;j<N;j++)
             {
                 arr1[j]=arr[(j+D)<N?(j+D):(j+D-N)];
             }
             for(int j= 0;j<N;j++)
             {
                 System.out.print(arr1[j]+" ");
             }
             System.out.println();
             T--;
         }
    }
}

Input:

1 (Number of Test Case)

5 3

1 2 3 4 5

Output:

4 5 1 2 3
answered Mar 4 by Priyaj
• 56,160 points

Related Questions In Java

0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,840 points
50 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
42 views
0 votes
1 answer

How to remove object from an Array?

Here is a code I came up ...READ MORE

answered Mar 11 in Java by Esha Gupta
34 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,040 points
27 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
42 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
32 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,320 points
288 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
201 views
0 votes
2 answers

How to sort array in descending order

there are some traditional ways that you ...READ MORE

answered Jul 4, 2018 in Java by Priyaj
• 56,160 points
25 views
0 votes
1 answer

How to iterate in a HashMap in Java?

You can use for but in a ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 56,160 points
39 views