• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
October 25, 2023 |940 Views
SDE Sheet - Count total set bits
  Share   Like
Description
Discussion

This video is part of the Bit Manipulation section under the GFG SDE Sheet.

In this problem, we are given, a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive).

Example :

Input: N = 4
Output: 5

Explanation:
For numbers from 1 to 4.
For 1: 0 0 1 = 1 set bits
For 2: 0 1 0 = 1 set bits
For 3: 0 1 1 = 2 set bits
For 4: 1 0 0 = 1 set bits
Therefore, the total set bits is 5.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/count-total-set-bits-in-all-numbers-from-1-to-n/
Problem: https://practice.geeksforgeeks.org/problems/count-total-set-bits-1587115620/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More