Sum of elements from an array having even parity
Given an array arr[], the task is to calculate the sum of the elements from the given array which has even parity i.e. the number of set bits is even using bitwise operator.
Examples:
Input: arr[] = {2, 4, 3, 5, 9}
Output: 17
Only 3(0011), 5(0101) and 9(1001) have even parity
So 3 + 5 + 9 = 17
Input: arr[] = {1, 5, 4, 16, 10}
Output: 15
Approach: Initialize a variable sum = 0 and traverse the array from 0 to n – 1 while counting the number of set bits in arr[i] using Brian Kernighan’s Algorithm. If the count is even then update sum = sum + arr[i]. Print the sum in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkEvenParity( int x)
{
int parity = 0;
while (x != 0) {
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true ;
else
return false ;
}
long sumlist( int a[], int n)
{
long sum = 0;
for ( int i = 0; i < n; i++) {
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
int main()
{
int arr[] = { 2, 4, 3, 5, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << sumlist(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkEvenParity( int x)
{
int parity = 0 ;
while (x != 0 ) {
x = x & (x - 1 );
parity++;
}
if (parity % 2 == 0 )
return true ;
else
return false ;
}
static long sumlist( int a[], int n)
{
long sum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
public static void main (String[] args) {
int arr[] = { 2 , 4 , 3 , 5 , 9 };
int n =arr.length;
System.out.println(sumlist(arr, n));
}
}
|
Python3
def checkEvenParity(x):
parity = 0
while (x ! = 0 ):
x = x & (x - 1 )
parity + = 1
if (parity % 2 = = 0 ):
return True
else :
return False
def sumlist(a, n):
sum = 0
for i in range (n):
if (checkEvenParity(a[i])):
sum + = a[i]
return sum
if __name__ = = '__main__' :
arr = [ 2 , 4 , 3 , 5 , 9 ]
n = len (arr)
print (sumlist(arr, n))
|
C#
using System ;
class GFG {
static bool checkEvenParity( int x)
{
int parity = 0;
while (x != 0) {
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true ;
else
return false ;
}
static long sumlist( int []a, int n)
{
long sum = 0;
for ( int i = 0; i < n; i++) {
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
public static void Main () {
int []arr = { 2, 4, 3, 5, 9 };
int n =arr.Length;
Console.WriteLine(sumlist(arr, n));
}
}
|
PHP
<?php
function checkEvenParity( $x )
{
$parity = 0;
while ( $x != 0)
{
$x = ( $x & ( $x - 1));
$parity ++;
}
if ( $parity % 2 == 0)
return true;
else
return false;
}
function sumlist( $a , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if (checkEvenParity( $a [ $i ]))
$sum += $a [ $i ];
}
return $sum ;
}
$arr = array ( 2, 4, 3, 5, 9 );
$n = sizeof( $arr );
echo sumlist( $arr , $n );
?>
|
Javascript
<script>
function checkEvenParity(x)
{
let parity = 0;
while (x != 0)
{
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true ;
else
return false ;
}
function sumlist(a, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
{
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
let arr = [ 2, 4, 3, 5, 9 ];
let n = arr.length;
document.write(sumlist(arr, n));
</script>
|
Time complexity: O(nlogn)
Auxiliary space: O(1)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...