Check if original Array Sum is Odd or Even using Bitwise AND of Array
Last Updated :
15 Sep, 2022
Given an integer N denoting the size of an array and the bitwise AND (K) of all elements of the array. The task is to determine whether the total sum of the elements is odd or even or cannot be determined.
Examples:
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
Approach: The solution to the problem is based on the below observation:
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.
Follow the steps mentioned below to solve the problem:
- If the LSB of K is 1, it clearly proves that every number has LSB as 1 i.e., every number is odd because if any single number has LSB 0 then LSB of K will be 0.
- The sum of N odd number is always odd if N is odd.
- The sum is even and if N is even.
- If the LSB of K is 0 the sum cannot be determined except for the case when N is 1 i.e. when N is 1 sum depends on the parity of K. In another case, it's impossible to find as the count of even and odd numbers cannot be determined.
Below is the implementation of the above approach.
C++
// C++ code to implement the above approach
#include <bits/stdc++.h>
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 is 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 is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code to implement the above approach
// Function to find whether the sum is even,
// odd or impossible to find.
const findtotalsum = (n, a) => {
// Separate case when N is 1
// as it depend on parity of n
if (n == 1) {
if (a % 2 == 0)
document.write("Even<br/>");
else
document.write("Odd<br/>");
}
// Check if a is odd
else if (a % 2 != 0) {
// Checking whether n is odd or even
if (n % 2 == 0)
document.write("Even<br/>");
else
document.write("Odd<br/>");
}
else {
// When no condition applies
// its impossible to find sum
document.write("Impossible<br/>");
}
}
// Driver code
let N, K;
N = 5, K = 4;
findtotalsum(N, K);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if a Number is Odd or Even using Bitwise Operators Given a number n, the task is to check whether the number is even or odd using Bitwise Operators.Examples: Input: n = 11 Output: OddInput: n = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even:1. Using Bitwise XOR operator: The idea is to check whether the l
6 min read
Queries to check whether bitwise AND of a subarray is even or odd Given an array arr[] of N positive integers, the task is to answer Q queries where each query consists of a range [L, R] and you have to check whether the bitwise AND of the elements from the given index range is even or odd.Examples: Input: arr[] = {1, 1, 2, 3}, Q[][] = {{1, 2}, {0, 1}} Output: Eve
10 min read
Check if the XOR of an array of integers is Even or Odd Given an array arr containing integers of size N, the task is to check if the XOR of this array is even or odd Examples: Input: arr[] = { 2, 4, 7} Output: Odd Explanation: XOR of array = 2 ^ 4 ^ 7 = 1, which is odd Input: arr[] = { 3, 9, 12, 13, 15 } Output: Even Naive Solution: First find the XOR o
5 min read
Sum of Bitwise OR of every array element paired with all other array elements Given an array arr[] consisting of non-negative integers, the task for each array element arr[i] is to print the sum of Bitwise OR of all pairs (arr[i], arr[j]) ( 0 ⤠j ⤠N ). Examples: Input: arr[] = {1, 2, 3, 4}Output: 12 14 16 22Explanation:For i = 0 the required sum will be (1 | 1) + (1 | 2) + (
11 min read
Check whether bitwise AND of N numbers is Even or Odd Given an array arr[] containing N numbers. The task is to check whether the bitwise-AND of the given N numbers is even or odd.Examples: Input: arr[] = { 2, 12, 20, 36, 38 } Output: Even Input: arr[] = { 3, 9, 17, 13, 15 } Output: Odd A Simple Solution is to first find the AND of the given N numbers,
7 min read