检查原始数组总和是奇数还是偶数使用数组的按位与
给定一个表示数组大小的整数N和数组所有元素的按位与(K) 。任务是确定元素的总和是奇数还是偶数或无法确定。
例子:
Input: N = 1, K = 11
Output: Odd
Explanation: As there is only one element in the array, the element itself is 11.
Input: N = 1, K = 2
Output: Even
Explanation: As there is only one element in the array. the element itself is 2.
Input: N = 5, K = 4
Output: Impossible
方法:该问题的解决方案基于以下观察:
Observation:
- We know that for bitwise AND to be 1, all the bits must be 1.
- So if the given LSB of given bitwise AND of Array is 1, it must mean that LSB of all the elements of Array must have been 1.
- On the other hand, if the LSB of given bitwise AND of Array is 0, it can mean that either all or some of the elements of Array has 0 in their LSB.
Conclusion: Therefore using above observation, it can be said that:
- For bitwise AND to be odd, all the elements of the array must be odd, as LSB of odd numbers is always 1.
- But if the bitwise AND is even, then nothing can be said about the parity (odd or even) of all the elements.
请按照以下步骤解决问题:
- 如果K的LSB为 1 ,则清楚地证明每个数字的 LSB 为 1 即每个数字都是奇数,因为如果任何单个数字的 LSB 为 0,则K的 LSB 将是 0。
- 如果N为奇数,则N个奇数之和总是奇数。
- 和是偶数,如果N是偶数。
- 如果K的LSB为 0,则无法确定总和,除非N 为 1的情况,即N为1时总和取决于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)