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, 2019 in Java 1,000 views

## 3 answers to this question.

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, 2019 by
• 58,080 points
```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, 2019 by
• 59,170 points
Muchas gracias. ?Como puedo iniciar sesion? answered May 2, 2020 by oisoucknfn

## How to test that an array contains a certain value?

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

+1 vote

## How to convert a List to an Array in Java?

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

## How to remove object from an Array?

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

## How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

+1 vote

## Are arrays equivalent to objects in Java ?

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

+1 vote

## 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

+1 vote

+1 vote

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

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