#Don't know why output given same input is different for two solutions

18 messages · Page 1 of 1 (latest)

faint mirage
#

given the input

20
1 3 5 7 9 11 13 15 17 19 21 2 4 6 8 10 12 14 16 18

and the two solutions (removed input class at the bottom)

public static void main(String[] args) throws IOException {
        Kattio io = new Kattio();
        int n = io.nextInt();
        int o = 0; int e = 0;
        for (int i = 0; i < n; i++) {
            int j = io.nextInt();
            if (j % 2 == 0) {e++;}
            else {o++;}
        }
        if (o == e || e == o + 1) {io.println(n);}
        else {
            while (o > e) {
                o -= 2; e++;
            }
            if (e > o + 1) {
                e = o + 1;
            }
        }
        io.println(e + o);
        io.close();
      
    }
public static void main(String[] args) throws IOException {
        Kattio io = new Kattio();
        int n = io.nextInt();
        int o = 0; int e = 0;
        for (int i = 0; i < n; i++) {
            int j = io.nextInt();
            if (j % 2 == 0) {e++;}
            else {o++;}
        }
        if (o == e || e == o + 1) {io.println(n);}
        else {
            if (e > o + 1) {io.println(o * 2 + 1);}
            else {
                while (o > e) {
                    o -= 2; e++;
                }
                io.println(o * 2 + 1);
            }
        }
        io.close();
      
    }```
why does the first one print the correct answer, 18, and the second print the wrong answer, 19?
I can't see how the second solution differs from the first solution.
The first one just seems like a "smarter" way to do it.
steel wedgeBOT
#

This post has been reserved for your question.

Hey @faint mirage! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

gloomy rock
#

what is the code even supposed to do

faint mirage
#

i dont think its relevant tbh

#

just dont know why the output is different

blazing thistle
#

The two codes differ in that they don't do the same things at the same time, notably checking whether things should be done. That usually lead to different results

faint mirage
#

its just the second else conditional

blazing thistle
#

Why would you think they do the same thing? They just don't

faint mirage
#

how??

blazing thistle
#

I already said, they don't do the same things at the same time. Notably checking whether things should be done

#

Order just matters

#

And conditions too

faint mirage
#

are you talking about how they are different in the sense that they are different solutions/different ways of doing the same thing?

#

because i know they are written differently, i just don't know why the second one outputs a different answer specifically

blazing thistle
#

It doesn't do the same thing. That's what happens when you shuffle when you do things and check things

#

You don't understand the concept of order?

#

If you think that they do the same thing, then just explain by what way they would do the same thing