The best way to make sure that at least two of the three boolean are true

Asked 2 years ago, Updated 2 years ago, 105 views

I was doing an interview and the interviewer told me to write a code to find out if two or more are true when there are three boolean variables

boolean atLeastTwo(boolean a, boolean b, boolean c) {
    if ((a && b) || (b && c) || (a && c)) {
        return true;
    }
    else{
        return false;
    }
}

So I made it like this, and the interviewer asked me if I could make it better, but is that possible?

java boolean boolean-logic

2022-09-22 14:58

1 Answers

boolean atLeastTwo(boolean a, boolean b, boolean c) {
    return a ? (b || c) : (b && c);
}


boolean atLeastTwo(boolean a, boolean b, boolean c) {
    return a && (b || c) || (b && c);
}

I think there's a way.


2022-09-22 14:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.