1

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?

maria
  • 45
  • 2
  • This does not appear to even attempt to generate your shift array. – Scott Hunter Mar 03 '23 at 13:25
  • thought i should move all even numbers to the front and then find out how many positions they moved, is my logic wrong? how should i approach the problem then? @ScottHunter – maria Mar 03 '23 at 13:27
  • You cannot shift positions in a Java Array. You could do that maybe with a `List`, but for an array, you just have to create a new array and put in the values at the correct positions. Another option would be that you first put all elements of your array into a list, modify the list and then convert it back to an array - but bad for two reasons: if this is a homework task or similar, you probably are not supposed to use `List`. If this is a "real" problem, I would rather work only with lists and not with arrays for such problems, so you don't have to convert. – cyberbrain Mar 03 '23 at 13:33
  • 1
    @cyberbrain: And yet the posted code *is* shifting the contents of the array (by swapping adjacent elements repeatedly). – Scott Hunter Mar 03 '23 at 13:34
  • What if the same even number appears multiple times? – Scott Hunter Mar 03 '23 at 13:35
  • Not that your code is *very* inefficient, if the order of the even and odd values does not have to be what this produces (for example, if {2,4,8,6,3,7,1,5} would be an acceptable result). And a more efficient algorithm could make computing your shift array very easy. – Scott Hunter Mar 03 '23 at 13:38
  • @ScottHunter could you please give me some hints on how to do write a more efficient algorithm? i'm a bit stuck with it (and a beginner with java), so some directions are really appreciated right now! – maria Mar 03 '23 at 13:44
  • @cyberbrain *You cannot shift positions in a Java Array.* It was clear (at least to me) that the OP did not mean a literal shift. Even if they were swapped, they still "shifted" their positions as they moved from one position to another. – WJS Mar 03 '23 at 15:02

2 Answers2

2

"How many times does this number need to shift" in this case is actually just "how many odd numbers in total are before this number", because you are trying to move all the even numbers to the beginning without changing the relative positions of the even numbers. Each even number needs to "move over" however many odd numbers there are before it.

So you just need to keep a cumulative count:

int[] a = { 1, 3, 2, 5, 4, 7, 8, 6 };
List<Integer> shifts = new ArrayList<>();
int oddNumberCount = 0;
for (int i : a) {
    if (i % 2 == 0) {
        shifts.add(oddNumberCount);
    } else {
        oddNumberCount++;
    }
}

I don't know how many even numbers there will be, so I used a list. If you require an array as the output you can convert it to a list simply like this (See Also):

int[] result = shifts.stream().mapToInt(x -> x).toArray();

If you do know how many even numbers there will be, you can of course just start with an array of that given size.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

What are you lacking in your code is you don't track what's the current position of the even numbers correctly:

public static void main(String...arg){
    int[] a={1,3,2,5, 4, 7, 8, 6,0}; //the array to be shifted
    
    int indexEvenNumber = 0;//where should we put the even number if we found it.
    for (int i = 0; i < a.length; i++) {
        if (a[i] % 2 == 0) {
            int temp = a[i];
            a[i] = a[indexEvenNumber];
            a[indexEvenNumber] = temp;
            indexEvenNumber++;
        }
    }
    // print the shifted array to confirm results
    Arrays.stream(a).forEach(System.out::println);

}
Karim
  • 1,004
  • 8
  • 18