Given an array **arr[]** of size **N**. The task is to find the sum of the digits of all array elements which contains even number of **1’s** in it’s their binary representation.

**Examples:**

Input :arr[] = {4, 9, 15}

Output :15

4 = 10, it contains odd number of 1’s

9 = 1001, it contains even number of 1’s

15 = 1111, it contains even number of 1’s

Total Sum = Sum of digits of9and15= 9 + 1 + 5 = 15

Input :arr[] = {7, 23, 5}

Output :10

**Approach :**

The number of 1’s in the binary representation of each array element is counted and if it is even then the sum of its digits is calculated.

Below is the implementation of the above approach:

## C++

`// CPP program to find Sum of digits with even ` `// number of 1’s in their binary representation ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count and check the ` `// number of 1's is even or odd ` `int` `countOne(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n) { ` ` ` `n = n & (n - 1); ` ` ` `count++; ` ` ` `} ` ` ` ` ` `if` `(count % 2 == 0) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Function to calculate the sum ` `// of the digits of a number ` `int` `sumDigits(` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(n != 0) { ` ` ` `sum += n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 4, 9, 15 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `total_sum = 0; ` ` ` ` ` `// Iterate through the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(countOne(arr[i])) ` ` ` `total_sum += sumDigits(arr[i]); ` ` ` `} ` ` ` ` ` `cout << total_sum << ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// C# program to find Sum of digits with even ` `// number of 1's in their binary representation ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count and check the ` `// number of 1's is even or odd ` `static` `int` `countOne(` `int` `n) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `n = n & (n - ` `1` `); ` ` ` `count++; ` ` ` `} ` ` ` ` ` `if` `(count % ` `2` `== ` `0` `) ` ` ` `return` `1` `; ` ` ` `else` ` ` `return` `0` `; ` `} ` ` ` `// Function to calculate the sum ` `// of the digits of a number ` `static` `int` `sumDigits(` `int` `n) ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` `sum += n % ` `10` `; ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `4` `, ` `9` `, ` `15` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` `int` `total_sum = ` `0` `; ` ` ` ` ` `// Iterate through the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(countOne(arr[i]) == ` `1` `) ` ` ` `total_sum += sumDigits(arr[i]); ` ` ` `} ` ` ` `System.out.println(total_sum); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find Sum of digits with even ` `# number of 1’s in their binary representation ` ` ` `# Function to count and check the ` `# number of 1's is even or odd ` `def` `countOne(n): ` ` ` `count ` `=` `0` ` ` `while` `(n): ` ` ` `n ` `=` `n & (n ` `-` `1` `) ` ` ` `count ` `+` `=` `1` ` ` ` ` `if` `(count ` `%` `2` `=` `=` `0` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `0` ` ` `# Function to calculate the summ ` `# of the digits of a number ` `def` `summDigits(n): ` ` ` `summ ` `=` `0` ` ` `while` `(n !` `=` `0` `): ` ` ` `summ ` `+` `=` `n ` `%` `10` ` ` `n ` `/` `/` `=` `10` ` ` ` ` `return` `summ ` ` ` `# Driver Code ` `arr ` `=` `[` `4` `, ` `9` `, ` `15` `] ` ` ` `n ` `=` `len` `(arr) ` `total_summ ` `=` `0` ` ` `# Iterate through the array ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(countOne(arr[i])): ` ` ` `total_summ ` `+` `=` `summDigits(arr[i]) ` ` ` `print` `(total_summ ) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find Sum of digits with even ` `// number of 1's in their binary representation ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count and check the ` `// number of 1's is even or odd ` `static` `int` `countOne(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `n = n & (n - 1); ` ` ` `count++; ` ` ` `} ` ` ` ` ` `if` `(count % 2 == 0) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Function to calculate the sum ` `// of the digits of a number ` `static` `int` `sumDigits(` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` `sum += n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] arr = { 4, 9, 15 }; ` ` ` ` ` `int` `n = arr.Length; ` ` ` `int` `total_sum = 0; ` ` ` ` ` `// Iterate through the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(countOne(arr[i]) == 1) ` ` ` `total_sum += sumDigits(arr[i]); ` ` ` `} ` ` ` `Console.WriteLine(total_sum); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

15

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Print numbers with digits 0 and 1 only such that their sum is N
- Next greater number than N with exactly one bit different in binary representation of N
- Number of mismatching bits in the binary representation of two integers
- Find consecutive 1s of length >= n in binary representation of a number
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Maximum number of consecutive 1's in binary representation of all the array elements
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Number of digits in the nth number made of given four digits
- Count different numbers possible using all the digits their frequency times
- Sort the numbers according to their product of digits
- Sum of even numbers at even position
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray

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.