educative.io

Java Exam - Separate even's and odd's

Can someone elaborate on why my output is not correct?
image


@Willy_Wijaya

Can you please provide the link of this particular lesson and your code? It seems like your code is not correct but at this point, your code and lesson link is required to analyze the issue.

Sure, Assessment link: Educative: Interactive Courses for Software Developers

It’s the last question from the assessment.

Source Code :

class javachallenge {
    public static ArrayList<Integer> separate(ArrayList<Integer> myList) {

        for (int i=0;i<myList.size();i++){
            if(myList.get(i) % 2 == 0){
                //if it's even, swap to left if the left data is odd
                for(int j = i-1; j>=0;j--){
                    if(Math.abs(myList.get(j)% 2) == 1){ // use abs for negative value
                        //swap
                        int temp = myList.get(j);
                        myList.set(j, myList.get(j+1));
                        myList.set(j+1, temp);
                    }
                }    
            }
        }

        return myList; 
    }
}

Output :

[2, 4, 6, 10, 3, 1, 7]
[-98, 100, 22, 12, 99, 33, 11]
[12, 76, 84, 55, 43, 23, 37]
[12, -23, 23, -13, 43, -99]
[-100, 2, 6, 8, 77, 5, 7, 9, 223]

^ All outputs are separated, even on the left side, odd on the right side

Hi @Willy_Wijaya,
I have checked your code and the question statement there isn’t any apparent issue in your code. Will look into it in detail and get back to you once we have an explanation for the failing test cases.