📌  相关文章
📜  检查原始数组总和是奇数还是偶数使用数组的按位与

📅  最后修改于: 2022-05-13 01:56:09.930000             🧑  作者: Mango

检查原始数组总和是奇数还是偶数使用数组的按位与

给定一个表示数组大小的整数N和数组所有元素的按位(K) 。任务是确定元素的总和是奇数还是偶数或无法确定。

例子:

方法:该问题的解决方案基于以下观察:

请按照以下步骤解决问题:

  • 如果KLSB为 1 ,则清楚地证明每个数字的 LSB 为 1 即每个数字都是奇数,因为如果任何单个数字的 LSB 为 0,则K的 LSB 将是 0。
    • 如果N为奇数,则N个奇数之和总是奇数。
    • 和是偶数,如果N是偶数。
  • 如果KLSB为 0,则无法确定总和,除非N 为 1的情况,即N1时总和取决于K的奇偶性。在另一种情况下,由于无法确定偶数和奇数的计数,因此无法找到。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to find whether the sum is even,
// odd or impossible to find.
void findtotalsum(int n, int a)
{
    // Separate case when N is 1
    // as it depend on parity of n
    if (n == 1) {
        if (a % 2 == 0)
            cout << "Even" << endl;
        else
            cout << "Odd" << endl;
    }
 
    // Check if a is odd
    else if (a % 2 != 0) {
 
        // Checking whether n is odd or even
        if (n % 2 == 0)
            cout << "Even" << endl;
        else
            cout << "Odd" << endl;
    }
    else {
 
        // When no condition applies
        // its impossible to find sum
        cout << "Impossible" << endl;
    }
}
 
// Driver code
int main()
{
    long int N, K;
    N = 5, K = 4;
    findtotalsum(N, K);
    return 0;
}


Java
// Java code to minimize number of rotations
import java.util.*;
 
class GFG {
 
  // Function to find whether the sum is even,
  // odd or impossible to find.
  static void findtotalsum(int n, int a)
  {
 
    // Separate case when N is 1
    // as it depend on parity of n
    if (n == 1) {
      if (a % 2 == 0)
        System.out.println("Even");
      else
        System.out.println("Odd");
    }
 
    // Check if a is odd
    else if (a % 2 != 0) {
 
      // Checking whether n is odd or even
      if (n % 2 == 0)
        System.out.println("Even");
      else
        System.out.println("Odd");
    }
    else {
 
      // When no condition applies
      // its impossible to find sum
      System.out.println("Impossible");
    }
  }
 
  // Driver code
  public static void main (String[] args) {
    int N = 5, K = 4;
    findtotalsum(N, K);
  }
}
 
// This code i contributed by hrithikgarg03188.


Python3
# Python program for the above approach
 
# Function to find whether the sum is even,
# odd or impossible to find.
def findtotalsum(n, a) :
 
    # Separate case when N is 1
    # as it depend on parity of n
    if (n == 1) :
        if (a % 2 == 0) :
            print("Even")
        else :
            print( "Odd" )
     
 
    # Check if a is odd
    elif (a % 2 != 0) :
 
        # Checking whether n is odd or even
        if (n % 2 == 0) :
            print("Even")
        else :
            print( "Odd" )
     
    else :
 
        # When no condition applies
        # its impossible to find sum
        print( "Impossible")
     
# Driver code
 
N = 5
K = 4
findtotalsum(N, K)
 
# This code is contributed by sanjoy_62.


C#
// C# code to minimize number of rotations
using System;
 
class GFG {
 
  // Function to find whether the sum is even,
  // odd or impossible to find.
  static void findtotalsum(int n, int a)
  {
 
    // Separate case when N is 1
    // as it depend on parity of n
    if (n == 1) {
      if (a % 2 == 0)
        Console.WriteLine("Even");
      else
        Console.WriteLine("Odd");
    }
 
    // Check if a is odd
    else if (a % 2 != 0) {
 
      // Checking whether n is odd or even
      if (n % 2 == 0)
        Console.WriteLine("Even");
      else
        Console.WriteLine("Odd");
    }
    else {
 
      // When no condition applies
      // its impossible to find sum
      Console.WriteLine("Impossible");
    }
  }
 
  // Driver code
  public static void Main () {
    int N = 5, K = 4;
    findtotalsum(N, K);
  }
}
 
// This code i contributed by Samim Hossain Mondal.


Javascript



输出
Impossible

时间复杂度: O(1)
辅助空间: O(1)