📌  相关文章
📜  检查N个数字的按位与是偶数还是奇数

📅  最后修改于: 2021-05-25 10:23:29             🧑  作者: Mango

给定一个包含N个数字的数组arr [] 。任务是检查给定N个数字的按位与是偶数还是奇数。
例子

一个简单的解决方案是首先找到给定N个数字的AND,然后检查此AND是偶数还是奇数。
更好的解决方案基于位操作和数学事实。

  • 任何两个偶数的按位与是偶数。
  • 任何两个奇数的按位与是一个奇数。
  • 偶数和奇数的按位与是偶数。

基于上述事实,可以推断出,如果数组中至少存在一个偶数,则整个数组的按位与将为奇数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to check if the bitwise AND
// of the array elements is even or odd
void checkEvenOdd(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0) {
 
            cout << "Even";
            return;
        }
    }
 
    cout << "Odd";
}
 
// Driver code
int main()
{
    int arr[] = { 2, 12, 20, 36, 38 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    checkEvenOdd(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
         
// Function to check if the bitwise AND
// of the array elements is even or odd
static void checkEvenOdd(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0)
        {
            System.out.print("Even");
            return;
        }
    }
    System.out.println("Odd");
}
 
// Driver code
public static void main (String[] args)
{
    int []arr = { 2, 12, 20, 36, 38 };
    int n = arr.length;
     
    checkEvenOdd(arr, n);
}
}
 
// This code is contributed by @tushil


Python3
# Python3 implementation of the approach
 
# Function to check if the bitwise AND
# of the array elements is even or odd
def checkEvenOdd(arr, n) :
 
    for i in range(n) :
 
        # If at least an even element is present
        # then the bitwise AND of the
        # array elements will be even
        if (arr[i] % 2 == 0) :
 
            print("Even", end = "");
            return;
 
    print("Odd", end = "");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 12, 20, 36, 38 ];
    n = len(arr);
 
    checkEvenOdd(arr, n);
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to check if the bitwise AND
// of the array elements is even or odd
static void checkEvenOdd(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0)
        {
            Console.Write ("Even");
            return;
        }
    }
    Console.Write ("Odd");
}
 
// Driver code
static public void Main ()
{
    int []arr = { 2, 12, 20, 36, 38 };
    int n = arr.Length;
 
    checkEvenOdd(arr, n);
}
}
 
// This code is contributed by ajit..


Javascript


输出:
Even

时间复杂度: O(N)