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++
// C++ program to find the sum of the elements // from an array which have even parity #include <bits/stdc++.h> using namespace std; // Function that returns true if x has even parity bool checkEvenParity(int x) { // We basically count set bits int parity = 0; while (x != 0) { x = x & (x - 1); parity++; } if (parity % 2 == 0) return true; else return false; } // Function to return the sum of the elements // from an array which have even parity long sumlist(int a[], int n) { long sum = 0; for (int i = 0; i < n; i++) { // If a[i] has even parity if (checkEvenParity(a[i])) sum += a[i]; } return sum; } // Driver code int main() { int arr[] = { 2, 4, 3, 5, 9 }; int n = sizeof(arr) / sizeof(arr[0]); cout << sumlist(arr, n); return 0; } |
Java
// Java program to find the sum of the elements // from an array which have even parity import java.io.*; class GFG { // Function that returns true if x has even parity static boolean checkEvenParity(int x) { // We basically count set bits int parity = 0; while (x != 0) { x = x & (x - 1); parity++; } if (parity % 2 == 0) return true; else return false; } // Function to return the sum of the elements // from an array which have even parity static long sumlist(int a[], int n) { long sum = 0; for (int i = 0; i < n; i++) { // If a[i] has even parity if (checkEvenParity(a[i])) sum += a[i]; } return sum; } // Driver code public static void main (String[] args) { int arr[] = { 2, 4, 3, 5, 9 }; int n =arr.length; System.out.println(sumlist(arr, n)); } } // This code is contributed by inder_verma.. |
Python3
# Python3 program to find the sum of the elements # from an array which have even parity # Function that returns true if x # has even parity def checkEvenParity(x): # We basically count set bits parity = 0 while (x != 0): x = x & (x - 1) parity += 1 if (parity % 2 == 0): return True else: return False # Function to return the sum of the elements # from an array which have even parity def sumlist(a, n): sum = 0 for i in range(n): # If a[i] has even parity if (checkEvenParity(a[i])): sum += a[i] return sum # Driver code if __name__ == '__main__': arr = [ 2, 4, 3, 5, 9 ] n = len(arr) print(sumlist(arr, n)) # This code is contributed by 29AjayKumar. |
C#
// C# program to find the sum of the elements // from an array which have even parity using System ; class GFG { // Function that returns true if x has even parity static bool checkEvenParity(int x) { // We basically count set bits int parity = 0; while (x != 0) { x = x & (x - 1); parity++; } if (parity % 2 == 0) return true; else return false; } // Function to return the sum of the elements // from an array which have even parity static long sumlist(int []a, int n) { long sum = 0; for (int i = 0; i < n; i++) { // If a[i] has even parity if (checkEvenParity(a[i])) sum += a[i]; } return sum; } // Driver code public static void Main () { int []arr = { 2, 4, 3, 5, 9 }; int n =arr.Length; Console.WriteLine(sumlist(arr, n)); } // This code is contributed by Ryuga } |
PHP
<?php // PHP program to find the sum of the // elements from an array which have // even parity // Function that returns true // if x has even parity function checkEvenParity($x) { // We basically count set bits $parity = 0; while ($x != 0) { $x = ($x & ($x - 1)); $parity++; } if ($parity % 2 == 0) return true; else return false; } // Function to return the sum of the elements // from an array which have even parity function sumlist($a, $n) { $sum = 0; for ($i = 0; $i < $n; $i++) { // If a[i] has even parity if (checkEvenParity($a[$i])) $sum += $a[$i]; } return $sum; } // Driver code $arr = array( 2, 4, 3, 5, 9 ); $n = sizeof($arr); echo sumlist($arr, $n); // This code is contributed by ajit. ?> |
17
Recommended Posts:
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find original array from encrypted array (An array of sums of other elements)
- Average of remaining elements after removing K largest and K smallest elements from array
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Program to find parity
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Finding the Parity of a number Efficiently
- Longest alternative parity subsequence
- Compute the parity of a number using XOR and table look-up
- Count the nodes in the given tree whose weight is even parity
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



