Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class ArrayElementShifter {
- private static boolean printDebug = true;
- private static int dimension = 10;
- private static int n = 3;
- /**
- * @param args
- */
- public static void main(String[] args) {
- //array initialization
- Integer[] initArray = new Integer[dimension];
- for(int i=0; i<dimension; i++) initArray[i] = i+1;
- //Move N elements to the end and print results
- System.out.println("\nResult: " + Arrays.toString(moveNElementsToTheEnd(initArray, n)));
- }
- public static final Object[] moveNElementsToTheEnd(Object[] initArray, int n){
- //for index calculation simplicity natural numbers (1,2,3...) are used;
- //for addressing array elements index-1 is used
- if(initArray.length % n != 0){
- int tmpIdx = 1, freeCellIdx = 1, nextValueIdx = 0;
- Object tmp = initArray[freeCellIdx -1];
- for(int i=0; i<initArray.length; i++){
- nextValueIdx = (freeCellIdx + n > initArray.length ? freeCellIdx - initArray.length : freeCellIdx) + n;
- if(nextValueIdx == tmpIdx){
- initArray[freeCellIdx-1] = tmp;
- } else{
- initArray[freeCellIdx-1] = initArray[nextValueIdx-1];
- }
- freeCellIdx = nextValueIdx;
- if(printDebug) {
- System.out.println("Debug:" + Arrays.toString(initArray));
- }
- }
- } else {
- int i =0;
- while(i<n){
- Object tmp = initArray[i];
- int j=0;
- while(i+n*(j+1) < initArray.length){
- initArray[i+n*j] = initArray[i+n*(j+1)];
- j++;
- if(printDebug) {
- System.out.println("Debug:" + Arrays.toString(initArray));
- }
- }
- initArray[i+n*j] = tmp;
- i++;
- if(printDebug) {
- System.out.println("Debug:" + Arrays.toString(initArray));
- }
- }
- }
- return initArray;
- }
- }
- Dim: 10; N:3
- Debug:[4, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Debug:[4, 2, 3, 7, 5, 6, 7, 8, 9, 10]
- Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 10]
- Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 3]
- Debug:[4, 2, 6, 7, 5, 6, 10, 8, 9, 3]
- Debug:[4, 2, 6, 7, 5, 9, 10, 8, 9, 3]
- Debug:[4, 2, 6, 7, 5, 9, 10, 8, 2, 3]
- Debug:[4, 5, 6, 7, 5, 9, 10, 8, 2, 3]
- Debug:[4, 5, 6, 7, 8, 9, 10, 8, 2, 3]
- Debug:[4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
- Result: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
- Dim: 13; N:3
- Debug:[4, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- Debug:[4, 2, 3, 7, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 10, 11, 12, 13]
- Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 13, 11, 12, 13]
- Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 13, 11, 12, 3]
- Debug:[4, 2, 6, 7, 5, 6, 10, 8, 9, 13, 11, 12, 3]
- Debug:[4, 2, 6, 7, 5, 9, 10, 8, 9, 13, 11, 12, 3]
- Debug:[4, 2, 6, 7, 5, 9, 10, 8, 12, 13, 11, 12, 3]
- Debug:[4, 2, 6, 7, 5, 9, 10, 8, 12, 13, 11, 2, 3]
- Debug:[4, 5, 6, 7, 5, 9, 10, 8, 12, 13, 11, 2, 3]
- Debug:[4, 5, 6, 7, 8, 9, 10, 8, 12, 13, 11, 2, 3]
- Debug:[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 11, 2, 3]
- Debug:[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
- Result: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
- Dim: 8; N:4
- Debug:[5, 2, 3, 4, 5, 6, 7, 8]
- Debug:[5, 2, 3, 4, 1, 6, 7, 8]
- Debug:[5, 6, 3, 4, 1, 6, 7, 8]
- Debug:[5, 6, 3, 4, 1, 2, 7, 8]
- Debug:[5, 6, 7, 4, 1, 2, 7, 8]
- Debug:[5, 6, 7, 4, 1, 2, 3, 8]
- Debug:[5, 6, 7, 8, 1, 2, 3, 8]
- Debug:[5, 6, 7, 8, 1, 2, 3, 4]
- Result: [5, 6, 7, 8, 1, 2, 3, 4]
Advertisement
Add Comment
Please, Sign In to add comment