Home > Software design >  Condition to check if exactactly 2 numbers in the Array of 3 elements are odd
Condition to check if exactactly 2 numbers in the Array of 3 elements are odd

Time:06-17

I have 3 values in an array.

How can I write an if statement to check if 2 of the 3 numbers are odd to print a message?

Code:

int[] test = {1, 2, 3};        
        
if (test[0] % 2 != 0 && test[1] % 2 != 0 &&
    test[2] % 2 != 0) {
            
    System.out.println("All numbers  odd");
} 
else if (condition) { //what will go here as a condition
    System.out.println("2 numbers odd");
}

So what can be used to check if 2 out of 3 numbers are odd?

CodePudding user response:

Try this.

int[] test = {1, 2, 3};

switch (IntStream.of(test).map(i -> i & 1).sum()) {
    case 3: System.out.println("All numbers odd"); break;
    case 2: System.out.println("2 numbers odd"); break;
}

output:

2 numbers odd

CodePudding user response:

So first we will be calculating all the possible scenarios :

  1. All even
  2. All odd
  3. 1 odd (2 even)
  4. 2 odd (1 even)

Then we use intStream functions to calculate the number of odds and even.

import java.util.stream.*;

public class MyClass {
    public static void main(String args[]) {
     int[] test = {2,5,3};
        
        if (IntStream.of(test).noneMatch(num -> num % 2 == 0)) // All odd
        {
            System.out.println("All numbers odd");
        }
        else if (IntStream.of(test).noneMatch(num -> num % 2 == 1)) // All even
        {
            System.out.println("All numbers even");
        }
        else if(IntStream.of(test).sum()%2==1)
        {
            System.out.println("1 number is odd");
        }
        else{
            System.out.println("2 numbers are odd");
        }
    }
}

CodePudding user response:

A very straightforward approach would be to count the number of odd element in the loop and then pass the number into a condition.

That's how it can be done in a concise way using Java 14 switch expressions:

public static void main(String[] args) {
    
    int[] test = {1, 2, 3};
    
    int oddCount = 0;
    
    for (int next : test) {
        if (next % 2 == 1) oddCount  ;
    }
    
    switch (oddCount) {
        case 1 -> System.out.println("1 number is odd");
        case 2 -> System.out.println("2 numbers are odd");
        case 3 -> System.out.println("All numbers are odd");
    }
}

Another way is to obtain a total of these numbers.

If exactly 2 of these 3 numbers are odd, it means that their sum should be even.

So it will be sufficient to check whether the sum is even and at least one of the first 2 array elements is odd:

public static void main(String[] args) {
    
    int[] test = {1, 2, 3};
    
    int sum = 0;
    
    for (int next : test) {
        sum  = next;
    }        
    
    if (sum % 2 == 1 && test[0] % 2 == 1 && test[1] % 2 == 1) {
        System.out.println("All numbers are odd");
    }
    else if (sum % 2 == 0 && (test[0] % 2 == 1 || test[1] % 2 == 1)) {
        System.out.println("2 numbers are odd");
    }
    else if (sum % 2 == 1) {
        System.out.println("1 number is odd");
    }
}

CodePudding user response:

The bitwise tests are nice because they work on positive or negative.

This test would only work for your else if clause because you have already eliminated the possibility they're all odd. This is literally checking all 3 combinations.

 boolean a = ( test[0]&test[1]&1 == 1 ) || (test[0]&test[2]&1 == 1) || (test[1] & test[2] & 1 == 1);

Another way that also requires the first test.

boolean b = (test[0]*test[1]*test[2])%4 != 0;

If two or more of the numbers are even, then the product is divisible by 4. So this will be true when two or more numbers are odd, but you've taken care of the 3 condition in your first clause. I'm not sure what happens when there is integer overflow though.

  • Related