public class RotateArray {
public static void main(String[] args) {
//e.g {0,1,2,3,4,5,6,7} N =4 should return {4,5,6,7,0,1,2,3}.
int[] input = {1,4,2,6,3,5,9,8};
int inputCopy[]=new int[input.length];;
int shiftingBy=2;
System.out.println("input.length"+input.length);
for (int i = 0; i<input.length; i++ )
{
inputCopy[getNextIndex(i,input.length,shiftingBy)]=input[i];
}
for(int i:inputCopy)
System.out.println(i);
}
public static int getNextIndex(int currentPosition, int arrLength, int shiftBy)
{
int reIndex=currentPosition-shiftBy;
if(reIndex<0)
{
return arrLength+reIndex;
}
return reIndex;
}
}
public static void main(String[] args) {
//e.g {0,1,2,3,4,5,6,7} N =4 should return {4,5,6,7,0,1,2,3}.
int[] input = {1,4,2,6,3,5,9,8};
int inputCopy[]=new int[input.length];;
int shiftingBy=2;
System.out.println("input.length"+input.length);
for (int i = 0; i<input.length; i++ )
{
inputCopy[getNextIndex(i,input.length,shiftingBy)]=input[i];
}
for(int i:inputCopy)
System.out.println(i);
}
public static int getNextIndex(int currentPosition, int arrLength, int shiftBy)
{
int reIndex=currentPosition-shiftBy;
if(reIndex<0)
{
return arrLength+reIndex;
}
return reIndex;
}
}
No comments:
Post a Comment