An algorithm which shifts each element of the Array to the right N times.

import java.util.*;

class Algorithm {

    public boolean isEmptyArray(int[] Arr ){
        if ( Arr == null || Arr.length == 0) {
            return true;
        }
        return false;
    }

    public boolean isAllValuesEquals(int[] Arr ){
        return Arrays.stream(Arr).distinct().count() == 1;
    }

    public boolean isNEqualsArrSize(int[] Arr, int N) {
        if ( N == 0 || N == Arr.length ||  N%Arr.length == 0 ) {
            return true;
        }
        return false;
    }

    public int[] joinArrays(int[] int1, int[] int2) {
        int[] result = new int[Arr.length];
        System.arraycopy( int1, 0, result, 0, int1.length);
        System.arraycopy( int2, 0, result, int1.length, int2.length );
        return result;
    }

    public int[] algorithm(int[] Arr, int N) {
        if ( isEmptyArray( Arr ) || isAllValuesEquals(Arr) || isNEqualsArrSize( Arr, N ) ) {
            return Arr;
        }
        int size = Arr.length;
        int pos = size - (N%size);
        int[] iniArr = Arrays.copyOfRange(Arr, pos , size);
        int[] endArr = Arrays.copyOfRange(Arr, 0, pos );
        return joinArrays(iniArr, endArr) ;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s