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, 2019 in Java by Sunita
2,125 views

3 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, 2019 by Priyaj
• 58,020 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, 2019 by Sirajul
• 59,230 points
0 votes
Muchas gracias. ?Como puedo iniciar sesion?
answered May 2, 2020 by oisoucknfn

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,920 points
1,103 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,190 points
1,137 views
0 votes
1 answer

How to remove object from an Array?

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

answered Mar 11, 2019 in Java by Esha Gupta
670 views
0 votes
1 answer

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

answered Jul 28, 2020 in Java by Niroj
• 82,840 points
2,025 views
+1 vote
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,620 points
1,326 views
+1 vote
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,290 points
1,185 views
+1 vote
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,680 points
3,739 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,220 points
3,578 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
• 58,020 points
1,182 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, 2019 in Java by Sirajul
• 59,230 points
1,021 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP