so I have been trying to solve an edabit question in Java.
I have a pretty good idea, though it doesn't work because the array is being passed by reference and not by value.
How can I fix it?
public class ShuffleCount {
public static int shuffleCount(int cardNum){
int[] arrayShuffle = new int[cardNum];
for (int i=0; i < cardNum; i++){
arrayShuffle[i] = i+1;
}
return shuffleCount(arrayShuffle, arrayShuffle, arrayShuffle, 0);
}
private static int shuffleCount(int[] firstOrder, int[] currOrder, int[] prevOrder, int shuffleCounter){
for (int i=0; i<firstOrder.length/2; i++){
currOrder[i*2] = prevOrder[i];
currOrder[i+1] = prevOrder[firstOrder.length/2 + i];
}
if(firstOrder == currOrder)
return shuffleCounter;
prevOrder = currOrder;
return shuffleCount(firstOrder, currOrder,prevOrder, shuffleCounter + 1);
}
}
The first function creates an array from 1 to the insert number.
Then passes it to the oder which solves the question using recursion. But the reference is passed and not the value.
How can it be solved?