How can I check if an array contains a pair of numbers whose product is odd?

How can I write a function that takes an array of integers and returns true if there is a pair of numbers whose product is odd?

What are the properties of odd integers? And of course, how do you write this function in Java? Also, maybe a short explanation of how you started formulating the algorithm for the actual implementation.

Yes, this is a function from the tutorial. No, this is not homework - I'm just trying to study, so please don't do your own homework.

+1


source to share


6 answers


An odd number is not evenly divisible by two. All you need to know is the two odd numbers in the set. Just check if each mod 2 number is nonzero. If so, then it is odd. If you find two odd numbers, you can multiply them to get another odd number.



Note: An odd number multiplied by an even number is always even.

+15


source


The product of two integers will be odd only if both integers are odd. So, to solve this problem, just scan the array once and see if there are two (or more) odd integers.



EDIT: As mentioned, you check if a number is odd using the modulus operator (%). If N% 2 == 0, then the number is even.

+6


source


Properties to think about:

  • Odd numbers are not divisible by 2
  • Any number multiplied by an even number is

So, you can re-ask the question like:

Does the array have at least two integers that are not divisible by 2?

Which should make it easier.

+3


source


You can check for uniformity (or oddness) with the module.

i% 2 = 0 if i is even; test for this and you can find out if the number is even / odd

0


source


Brute Force Algorithm:

public static boolean hasAtLeastTwoOdds(int[] args) {
    int[] target = args; // make defensive copy
    int oddsFound;
    int numberOddsSought = 2;

    for (int i = 0; i < target.length; i++) {
        if (target[i] % 2 != 0) {
            if (oddsFound== numberOddsSought) {
                return true;
            }
            oddsFound++;
        }
    }

    return false;
}

      

0


source


Thanks for your responses and comments.

Now I understand well how to check if an integer is odd. For example, this method is a neat way of doing this test without using the multiplication, modulus, or division operators:

 protected boolean isOdd(int i) {
    return ( (i&1) == 1);

}

      

With your help, I now understand that the problem is much simpler than I expected. Here is the rest of my Java implementation. Comments and criticism are welcome.

protected boolean isOddProduct(int[] arr) {
        int oddCount = 0;
        if (arr.length < 2) 
            throw new IllegalArgumentException();
        for (int i = 0; i <= arr.length-1; i++) {
            if (isOdd(arr[i]))
                oddCount++; 
        }
        return oddCount > 1;
    }

      

I wonder if there are other ways to do this test without using the *,%, or /? Perhaps I will ask this question in a new thread.

0


source







All Articles