Sum of digits with even number of 1's in their binary representation - GeeksforGeeks

17 Mar, 2021
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 of 9 and 15 = 9 + 1 + 5 = 15Input : 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++
Java
Python3
C#
Javascript
// 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 oddint 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 numberint sumDigits(int n){    int sum = 0;    while (n != 0) {        sum += n % 10;        n /= 10;    }     return sum;} // Driver Codeint 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;}
// Java program to find Sum of digits with even// number of 1's in their binary representationimport java.util.*; class GFG{ // Function to count and check the// number of 1's is even or oddstatic 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 numberstatic int sumDigits(int n){    int sum = 0;    while (n != 0)    {        sum += n % 10;        n /= 10;    }     return sum;} // Driver Codepublic 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
# 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 odddef 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 numberdef summDigits(n):    summ = 0    while (n != 0):        summ += n % 10        n //= 10     return summ # Driver Codearr = [4, 9, 15] n = len(arr)total_summ = 0 # Iterate through the arrayfor i in range(n):    if (countOne(arr[i])):        total_summ += summDigits(arr[i]) print(total_summ ) # This code is contributed by Mohit Kumar
// C# program to find Sum of digits with even// number of 1's in their binary representationusing System; class GFG{ // Function to count and check the// number of 1's is even or oddstatic 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 numberstatic int sumDigits(int n){    int sum = 0;    while (n != 0)    {        sum += n % 10;        n /= 10;    }    return sum;} // Driver Codepublic 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
<script>     // Javascript 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    function countOne(n)    {        let 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    function sumDigits(n)    {        let sum = 0;        while (n != 0) {            sum += n % 10;            n = parseInt(n / 10, 10);        }         return sum;    }         let arr = [ 4, 9, 15 ];           let n = arr.length;    let total_sum = 0;       // Iterate through the array    for (let i = 0; i < n; i++) {        if (countOne(arr[i]))            total_sum += sumDigits(arr[i]);    }       document.write(total_sum); </script>
15
 
mohit kumar 29
29AjayKumar
Code_Mech
nidhi_biet
divyeshrabadiya07
Numbers
Arrays
Competitive Programming
Arrays
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Fast I/O for Competitive Programming