The following implementation in JAVA uses the magical modulo operator in order to find the index of the array location to copy.
public int[] nLeftShift( int[] data , int n){
int N = data.length; // Size of an Array
n = n%N; // N shift operations means no shift operation.
int[] data2 = new int[N];
for(int i = 0; i< N ;i++){
data2[i] = data[(i+n)%N];
}
return data2;
}
public int[] nLeftShift( int[] data , int n){
int N = data.length; // Size of an Array
n = n%N; // N shift operations means no shift operation.
int[] data2 = new int[N];
for(int i = 0; i< N ;i++){
data2[i] = data[(i+n)%N];
}
return data2;
}
No comments:
Post a Comment