I'm writing a code that moves all even numbers to the front of the array and odd numbers to the back. So when an array looks like this:
int[] a={1,3,2,5, 4, 7, 8, 6};
the output should look like:
int[] b={2,4,8,6, 1, 3, 5, 7};
my problem is that the programme should return an array of positions that every even number moved to get to these positions. with my example the output should look like this: [2, 3, 4, 4]
shiftArray = {2,3,4,4};
as number 2 moved two positions, number 4 three positions etc.
here is what i have so far:
int temp=0;
int a=0;
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0){
for (int j=i;j>a;j--){
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
a++;
}
return arr;
}
how can i use what i have so far to get my desired output?