I'm trying to solve a simple array problem and I have encountered some bug.
I input {1, 2, 3, 4, 5, 6}
and rotate left by 2, it should give me {3, 4, 5, 6, 1, 2}
I cannot find a reason why it gives me {0, 0, 0, 0, 1, 2}
I think my code is correct. I do not like looking at answers but I looked it up but then I still don't see why it is giving me this wrong answer.
I've looked it through more than 1 hour and I still cannot figure it out so I joined this community. Please help me out...
public int[] rotate(int[] arr, int shift, int size) {
int[] output = new int[size];
for(int i = 0; i < output.length;i++) {
int newIndex = (arr.length-shift+i) % arr.length;
output[newIndex] = arr[i];
}
return output;
}
My output: