Open In App

Python map function | Count total set bits in all numbers from 1 to n

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples:

Input: n = 3
Output:  4
Binary representations are 1, 2 and 3
1, 10 and 11 respectively. Total set
bits are 1 + 1 + 2 = 4.

Input: n = 6
Output: 9

Input: n = 7
Output: 12

Input: n = 8
Output: 13

We have existing solution for this problem please refer Count total set bits in all numbers from 1 to n link. We can solve this problem in python using map() function. Approach is very simple,

  1. Write a function which first converts number into binary using bin(num) function and returns count of set bits in it.
  2. Map user defined function on list of numbers from 1 to n and we will get list of individual count of set bits in each number.
  3. Sum up count of all set bits.

Python3




# Function to Count total set bits in all numbers
# from 1 to n
 
# user defined function
def countSetBit(num):
 
     # convert decimal value into binary and
     # count all 1's in it
     binary = bin(num)
 
     return len([ch for ch in binary if ch=='1'])
 
# function which count set bits in each number
def countSetBitAll(input):
     
    # map count function on each number
    print (sum(map(countSetBit,input)))
 
# Driver program
if __name__ == "__main__":
    n = 8
    input=[]
    for i in range(1,n+1):
         input.append(i)
    countSetBitAll(input)


Output:

13

Time Complexity : O(log n)

Auxiliary Space: O(log n)

Another Approach:

The approach can be made more efficient using a lambda function, and the count() method in order to count the set bits in the binary form, as outlined below:

Python3




# Function to Count total set bits in all numbers
# from 1 to n
 
 
#Get the sum of all set bits
#in the range [1, n]
def countSetBitAll(n):
    # map count function on each number
    print(sum(map(lambda x: bin(x).count("1"), range(1, n + 1))))
 
 
# Driver program
n = 8
 
#Function Call
countSetBitAll(n)
 
 
#This code is contributed by phasing17


Output

13

Time Complexity : O(1 )

Auxiliary Space : O(1)



Similar Reads

Count total set bits in first N Natural Numbers (all numbers from 1 to N)
Given a positive integer N, the task is to count the total number of set bits in binary representation of all numbers from 1 to N. Examples: Input: N = 3Output: 4Explanation: Numbers from 1 to 3: {1, 2, 3}Binary Representation of 1: 01 -> Set bits = 1Binary Representation of 2: 10 -> Set bits = 1Binary Representation of 3: 11 -> Set bits =
15+ min read
Count total set bits in all numbers from 1 to n | Set 2
Given a positive integer N, the task is to count the sum of the number of set bits in the binary representation of all the numbers from 1 to N.Examples: Input: N = 3 Output: 4 DecimalBinarySet Bit Count101121013112 1 + 1 + 2 = 4Input: N = 16 Output: 33 Recommended PracticeCount total set bitsTry It! Approach: Some other approaches to solve this pro
11 min read
Count total set bits in all numbers from 1 to N | Set 3
Given a positive integer N, the task is to count the total number of set bits in binary representation of all the numbers from 1 to N. Examples: Input: N = 3 Output: 4 setBits(1) + setBits(2) + setBits(3) = 1 + 1 + 2 = 4 Input: N = 6 Output: 9 Approach: Solution to this problem has been published in the Set 1 and the Set 2 of this article. Here, a
12 min read
Count of pairs {X, Y} from an array such that sum of count of set bits in X ⊕ Y and twice the count of set bits in X & Y is M
Given an array arr[] consisting of N non-negative integers and an integer M, the task is to find the count of unordered pairs {X, Y} of array elements that satisfies the condition setBits(X ⊕ Y) + 2 * setBits(X & Y) = M, where ⊕ denotes the Bitwise XOR and & denotes the Bitwise AND. Examples: Input: arr[] = {30, 0, 4, 5 }, M = 2Output: 2Exp
14 min read
Count total set bits in all numbers from range L to R
Given two positive integers L and R, the task is to count the total number of set bits in the binary representation of all the numbers from L to R. Examples: Input: L = 3, R = 5 Output: 5 Explanation: (3)10 = (11)2, (4)10 = (100)2, (5)10 = (101)2 So, Total set bit in range 3 to 5 is 5 Input: L = 10, R = 15 Output: 17 Method 1 - Naive Approach: The
15+ min read
Count total unset bits in all the numbers from 1 to N
Given a positive integer N, the task is to count the total number of unset bits in the binary representation of all the numbers from 1 to N. Note that leading zeroes will not be counted as unset bits.Examples: Input: N = 5 Output: 4 IntegerBinary RepresentationCount of unset bits110210131104100251011 0 + 1 + 0 + 2 + 1 = 4Input: N = 14 Output: 17 Ap
4 min read
Minimum number N such that total set bits of all numbers from 1 to N is at-least X
Given a number X, the task is to find the minimum number N such that the total set bits of all numbers from 1 to n is at least X. Examples: Input: x = 5 Output: 4 Set bits in 1-> 1 Set bits in 2-> 1 Set bits in 3-> 2 Set bits in 4-> 1 Hence first four numbers add upto 5 Input: x = 20 Output: 11 Approach: Use binary search to get the min
7 min read
Flip bits of the sum of count of set bits of two given numbers
Given two numbers A and B, the task is to count the number of set bits in A and B and flip the bits of the obtained sum. Examples: Input: A = 5, B = 7Output: 2Explanation:Binary representation of A is 101.Binary representation of B is 111.Count of set bits in A and B = 2 + 3 = 5.Binary representation of the sum obtained = 101Flipping the bits of th
5 min read
Count total set bits in an array
Given an array arr, the task is to count the total number of set bits in all numbers of that array arr. Example: Input: arr[] = {1, 2, 5, 7}Output: 7Explanation: Number of set bits in {1, 2, 5, 7} are {1, 1, 2, 3} respectively Input: arr[] = {0, 4, 9, 8}Output: 4 Approach: Follow the below steps to solve this problem: Create a variable cnt to store
5 min read
Print numbers having first and last bits as the only set bits
Given a positive integer n. The problem is to print numbers in the range 1 to n having first and last bits as the only set bits.Examples: Input : n = 10 Output : 1 3 5 9 (1)10 = (1)2. (3)10 = (11)2. (5)10 = (101)2. (9)10 = (1001)2 Naive Approach: Print "1". Now for i = 3 to n, check if (i-1) is a Perfect power of two or not. If true then print i. C
10 min read
Article Tags :
Practice Tags :