Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array 1D right rotation in Java

Tags:

java

arrays

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));
    }

}
like image 219
Rock910 Avatar asked Dec 06 '25 18:12

Rock910


1 Answers

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.

like image 153
Lothar Avatar answered Dec 08 '25 06:12

Lothar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!