2

I have an array and I would like to shift the array elements by n-indexes. For simplicity of the problem, let's say, I would like to shift by 1-index and then, I can write a while or recursion to make it n-times.

Say, the array is A = [3, 8, 9, 7, 6] and I would like to right shift by 1-index to make it A = [6, 3, 8, 9, 7]

I think of an HashMap that will take indexes and will shift by n-indexes. Say,

Map<Integer, Integer> map = new HashMap<>();
int n = 2;

for(int j =0; j < arr.length; j++){
     if(j+2 > arr.length -1){
         map.put(j+2 - arr.length, arr[j]);
     }
     map.put(j+2, arr[j]);
}

for(Map.Entry<Integer, Integer> map : amp.entrySet()){
     arr[map.getKey()] = map.getValue();
}

The solution doesn't feel very good to me. How to write the algorithm for it ?

Coder
  • 1,917
  • 3
  • 17
  • 33
Chak
  • 75
  • 1
  • 12

3 Answers3

1
import com.sun.tools.javac.util.ArrayUtils;    
import java.util.Arrays;

public class shift {
    public static void main(String args[]){
        int [] arr = {3, 8, 9, 7, 6};
        int index = 1; // you can change it to whatever you want based on ur ques
        int temp;
        int len = arr.length;
        int[] left = Arrays.copyOfRange(arr, 0, len-index);
        int[] right = Arrays.copyOfRange(arr,len-index,len);
        int[] result = new int[5];
        System.arraycopy(right, 0, result, 0, right.length);
        System.arraycopy(left, 0, result, right.length, left.length);
        System.out.println(Arrays.toString(result));
    }
}
clever_bassi
  • 2,392
  • 2
  • 24
  • 43
0

You can use simple method like below where it will take array and the number of shifts as input. I have added inline comments to describe each step.

    static String[] shiftArray(String[] inputArray, int shifts) {

    // Loop the number of shifts 
    for (int x = 0; x < shifts; x++) {

        //Last element will be store in temporary variable 
        String lastElm = inputArray[inputArray.length - 1];

        //This loop will shift array content by one
        for (int i = inputArray.length; i > 0; i--) {
            if (i == 1) {
                inputArray[0] = lastElm;
            } else {
                inputArray[i - 1] = inputArray[i - 2];

            }
        }
    }

    return inputArray;
   }
Coder
  • 1,917
  • 3
  • 17
  • 33
0

I provides 2 solutions here where the 1st one s colected from the forum.

/*solution-a*/
    public static int swap(int itself , int dummy){
        return itself;
    }

    public static void reverse(int[] A, int start, int end){

        int i = start, j = end;

        while(i < j){

            A[i] = swap(A[j], A[j] = A[i]);

            i++;
            j--;
        }
    }

    public int[] solution(int[] A, int K) {

        if(K > A.length){
            return null;
        }

        reverse(A, 0, A.length -1);
        reverse(A, 0, K-1);
        reverse(A, K, A.length -1);

        return A;
    }
    /*ENd of solution-a*/






    /*ENd of solution-b*/
    public static int[] solution1(int[] arr, int n){

        Map<Integer, Integer> map = new HashMap<>();

        for(int j =0; j < arr.length; j++){

            if(j+n > arr.length -1){

                map.put(j+n - arr.length, arr[j]);
                continue;
            }

            map.put(j+n, arr[j]);
        }

        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            arr[entry.getKey()] = entry.getValue();
        }

        return arr;
    }
    /*ENd of solution-b*/
Chak
  • 75
  • 1
  • 12