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++
#include <bits/stdc++.h>
using namespace std;
void findtotalsum( int n, int a)
{
if (n == 1) {
if (a % 2 == 0)
cout << "Even" << endl;
else
cout << "Odd" << endl;
}
else if (a % 2 != 0) {
if (n % 2 == 0)
cout << "Even" << endl;
else
cout << "Odd" << endl;
}
else {
cout << "Impossible" << endl;
}
}
int main()
{
long int N, K;
N = 5, K = 4;
findtotalsum(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void findtotalsum( int n, int a)
{
if (n == 1 ) {
if (a % 2 == 0 )
System.out.println( "Even" );
else
System.out.println( "Odd" );
}
else if (a % 2 != 0 ) {
if (n % 2 == 0 )
System.out.println( "Even" );
else
System.out.println( "Odd" );
}
else {
System.out.println( "Impossible" );
}
}
public static void main (String[] args) {
int N = 5 , K = 4 ;
findtotalsum(N, K);
}
}
|
Python3
def findtotalsum(n, a) :
if (n = = 1 ) :
if (a % 2 = = 0 ) :
print ( "Even" )
else :
print ( "Odd" )
elif (a % 2 ! = 0 ) :
if (n % 2 = = 0 ) :
print ( "Even" )
else :
print ( "Odd" )
else :
print ( "Impossible" )
N = 5
K = 4
findtotalsum(N, K)
|
C#
using System;
class GFG {
static void findtotalsum( int n, int a)
{
if (n == 1) {
if (a % 2 == 0)
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
else if (a % 2 != 0) {
if (n % 2 == 0)
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
else {
Console.WriteLine( "Impossible" );
}
}
public static void Main () {
int N = 5, K = 4;
findtotalsum(N, K);
}
}
|
Javascript
<script>
const findtotalsum = (n, a) => {
if (n == 1) {
if (a % 2 == 0)
document.write( "Even<br/>" );
else
document.write( "Odd<br/>" );
}
else if (a % 2 != 0) {
if (n % 2 == 0)
document.write( "Even<br/>" );
else
document.write( "Odd<br/>" );
}
else {
document.write( "Impossible<br/>" );
}
}
let N, K;
N = 5, K = 4;
findtotalsum(N, K);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...