Rock910
Rock910

Reputation: 41

Array 1D right rotation in Java

I wish to rotate the elements of an array k times to the right(where k is an int), the array is of size n(int n), You can see my code for this below, I'm unsure of how to proceed when array[n+k] hits a[n-1]

I'm trying to wrap the a[n-1] around to be a[0] and then continue the rotation as needed. I'm aware others have solved this in different ways but I'm asking utilizing my code and adjusting it how it can work.

My code:

package com.company;
//Make an array that rotates n members k times
import java.util.Scanner;
import java.util.Arrays;
public class RightRotationArray {
    public static void main(String[] args) {
        //I want an array with a size n
        int n;
        int k;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a size for the array: ");
        n= input.nextInt();
        System.out.print("Enter in a shift to the array: ");
        k= input.nextInt();
        //Declare the array
        int[] array1= new int[n];
        //now input elements into the array
        for(int i=0; i< n; i++){
            System.out.print("Enter in a value: ");
            array1[i]= input.nextInt();
            //want to make the array right rotate so everyone is shifted right
            for(int a= 0; a<n-1; a++){
                array1[a]= array1[a+k];//shifting step
                //need an if to catch the above size declaration
                if(array1[a+k]== array1[n-1]){
                    array1[n-1] = array1[0];

                }
            }
        }
        //print out the array to verify
        System.out.println(Arrays.toString(array1));
    }

}

Upvotes: 3

Views: 409

Answers (2)

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4089

Here is a simple bit of math to rotate the array, just note that you should always check what the result of adding your shift to your index before trying to access it. In your example you assume you can access array1[a+k] which is not always the case. You'll also need to account for shift values greater than the length of the array created, therefore the % is used to ensure valid indices.

        int[] array1 = {1,6,8,4,9,2,0,-1};
        int[] arrayShifted = new int[array1.length];
        int shift = input.nextInt();

        for(int i = 0; i < array1.length; i++)
        {
            if(i + shift < array1.length)
                arrayShifted[i+shift] = array1[i];
            else
                arrayShifted[Math.abs(array1.length - (i + shift))%array1.length] = array1[i];
        }

Output

Shift = 0 //sanity check
1 6 8 4 9 2 0 -1

Shift = 2
0 -1 1 6 8 4 9 2

Shift = 5
4 9 2 0 -1 1 6 8

Shift = 16 //rotated length*2 so values should remain in their same location
1 6 8 4 9 2 0 -1

Shift = 17
-1 1 6 8 4 9 2 0

Upvotes: 1

Lothar
Lothar

Reputation: 5449

import java.util.Arrays;

public class ShiftArray {

    public final static void main(String[] args) {
        int steps = 5;
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        rotateArray(array, steps);
        Arrays.stream(array)
            .forEach(System.out::println);

        // alternate approach doing the shifring while filling:

        array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] newArray = new int[array.length];
        for (int i = 0; i < newArray.length; i++) {
            int index = (i + steps) % newArray.length;
            newArray[index] = array[i];
        }

        System.out.println("================");

        Arrays.stream(newArray)
        .forEach(System.out::println);

    }

    public static void rotateArray(int[] array, int steps) {
        int[] tempArray = new int[steps];
        System.arraycopy(array, array.length - steps, tempArray, 0, steps);
        for (int i = array.length - steps - 1; i >= 0; i--) {
            array[i + steps] = array[i];
        }
        System.arraycopy(tempArray, 0, array, 0, steps);
    }

}

Depending on the size of the array and the value of steps the first solution might lead to memory problems.

Upvotes: 1

Related Questions