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
29 views

2 answers 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,900 points
0 votes
private static void rotate(int[] arr, int order) {
    if (arr == null || order < 0) {
        throw new IllegalArgumentException("The array must be non-null and the order must be non-negative");
    }
    int offset = arr.length - order % arr.length;
    if (offset > 0) {
        int[] copy = arr.clone();
        for (int i = 0; i < arr.length; ++i) {
            int j = (i + offset) % arr.length;
            arr[i] = copy[j];
        }
    }
}
answered Aug 29 by Sirajul
• 31,980 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,880 points
65 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
62 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
43 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,050 points
52 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
69 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
58 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,640 points
482 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
473 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,900 points
47 views
0 votes
2 answers

How to iterate in a HashMap in Java?

Iterating using Iterator. Using Generics: Map<Integer, Integer> map = ...READ MORE

answered Aug 28 in Java by Sirajul
• 31,980 points
53 views