Sum of digits with even number of 1’s in their binary representation
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 = 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++
#include <bits/stdc++.h>
using namespace std;
int countOne( int n)
{
int count = 0;
while (n) {
n = n & (n - 1);
count++;
}
if (count % 2 == 0)
return 1;
else
return 0;
}
int sumDigits( int n)
{
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main()
{
int arr[] = { 4, 9, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
int total_sum = 0;
for ( int i = 0; i < n; i++) {
if (countOne(arr[i]))
total_sum += sumDigits(arr[i]);
}
cout << total_sum << '\n' ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
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 ;
}
static int sumDigits( int n)
{
int sum = 0 ;
while (n != 0 )
{
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
public static void main(String[] args)
{
int arr[] = { 4 , 9 , 15 };
int n = arr.length;
int total_sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (countOne(arr[i]) == 1 )
total_sum += sumDigits(arr[i]);
}
System.out.println(total_sum);
}
}
|
Python3
def countOne(n):
count = 0
while (n):
n = n & (n - 1 )
count + = 1
if (count % 2 = = 0 ):
return 1
else :
return 0
def summDigits(n):
summ = 0
while (n ! = 0 ):
summ + = n % 10
n / / = 10
return summ
arr = [ 4 , 9 , 15 ]
n = len (arr)
total_summ = 0
for i in range (n):
if (countOne(arr[i])):
total_summ + = summDigits(arr[i])
print (total_summ )
|
C#
using System;
class GFG
{
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;
}
static int sumDigits( int n)
{
int sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
public static void Main()
{
int [] arr = { 4, 9, 15 };
int n = arr.Length;
int total_sum = 0;
for ( int i = 0; i < n; i++)
{
if (countOne(arr[i]) == 1)
total_sum += sumDigits(arr[i]);
}
Console.WriteLine(total_sum);
}
}
|
Javascript
<script>
function countOne(n)
{
let count = 0;
while (n) {
n = n & (n - 1);
count++;
}
if (count % 2 == 0)
return 1;
else
return 0;
}
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;
for (let i = 0; i < n; i++) {
if (countOne(arr[i]))
total_sum += sumDigits(arr[i]);
}
document.write(total_sum);
</script>
|
Approach#2: Using bin()
Traverse the array and check the binary representation of each element. If the count of 1’s in the binary representation of an element is even, add the sum of its digits to the answer variable. Return the answer variable.
Algorithm
1. Start with an answer variable set to 0.
2. Traverse the given array.
3. For each number in the array, convert it to binary using the in-built bin() function and remove the 0b prefix from the binary representation using the string slice binary[2:].
4. Count the number of ones in the binary representation of the current number using the string method count().
5. If the count of ones is even, add the sum of the digits of the current number to the answer variable using the sum() and map() functions.
6. Return the final answer variable.
C++
#include <iostream>
#include <vector>
#include <bitset>
int sumOfDigitsWithEvenOnes(std::vector< int > arr) {
int ans = 0;
for ( int num : arr) {
std::bitset<32> binary(num);
int countOnes = binary.count();
if (countOnes % 2 == 0) {
int numCopy = num;
int digitSum = 0;
while (numCopy > 0) {
digitSum += numCopy % 10;
numCopy /= 10;
}
ans += digitSum;
}
}
return ans;
}
int main() {
std::vector< int > arr = {4, 9, 15};
int result = sumOfDigitsWithEvenOnes(arr);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
static int
sumOfDigitsWithEvenOnes(ArrayList<Integer> arr)
{
int ans = 0 ;
for ( int num : arr) {
String binaryString
= Integer.toBinaryString(num);
int countOnes = Integer.bitCount(num);
if (countOnes % 2 == 0 ) {
int numCopy = num;
int digitSum = 0 ;
while (numCopy > 0 ) {
digitSum += numCopy % 10 ;
numCopy /= 10 ;
}
ans += digitSum;
}
}
return ans;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>();
arr.add( 4 );
arr.add( 9 );
arr.add( 15 );
int result = sumOfDigitsWithEvenOnes(arr);
System.out.println(result);
}
}
|
Python3
def sum_of_digits_with_even_ones(arr):
ans = 0
for num in arr:
binary = bin (num)[ 2 :]
count_ones = binary.count( '1' )
if count_ones % 2 = = 0 :
ans + = sum ( map ( int , str (num)))
return ans
arr = [ 4 , 9 , 15 ]
print (sum_of_digits_with_even_ones(arr))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int CountSetBits( int num)
{
int count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
static int SumOfDigitsWithEvenOnes(List< int > arr)
{
int ans = 0;
foreach ( int num in arr)
{
int countOnes = CountSetBits(num);
if (countOnes % 2 == 0) {
int numCopy = num;
int digitSum = 0;
while (numCopy > 0) {
digitSum += numCopy % 10;
numCopy /= 10;
}
ans += digitSum;
}
}
return ans;
}
static void Main()
{
List< int > arr = new List< int >{ 4, 9, 15 };
int result = SumOfDigitsWithEvenOnes(arr);
Console.WriteLine(result);
Console.ReadLine();
}
}
|
Javascript
function sumOfDigitsWithEvenOnes(arr) {
let ans = 0;
for (let num of arr) {
let binary = num.toString(2);
let countOnes = (binary.match(/1/g) || []).length;
if (countOnes % 2 === 0) {
let numCopy = num;
let digitSum = 0;
while (numCopy > 0) {
digitSum += numCopy % 10;
numCopy = Math.floor(numCopy / 10);
}
ans += digitSum;
}
}
return ans;
}
function main() {
let arr = [4, 9, 15];
let result = sumOfDigitsWithEvenOnes(arr);
console.log(result);
}
main();
|
Time complexity: O(N*M), where N is the length of the array and M is the maximum number of bits in any element of the array.
Space complexity: O(1).
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...