-->

12/31/20

Count Set Bits GFG problems solution

https://practice.geeksforgeeks.org/problems/set-bits0143/1

Problem statement- 

Given a positive integer N, print count of set bits in it. 

Example 1:

Input:
N = 6
Output:
2
Explanation:
Binary representation is '110' 
So the count of the set bit is 2.

Example 2:

Input:
8
Output:
1
Explanation:
Binary representation is '1000' 
So the count of the set bit is 1.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function setBits() which takes an Integer N and returns the count of number of set bits.

Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 109

count set bit



SOlution -

If we do AND operation 
1&1=1
1&0=0
by using above property 
we will perform and operation with current number if  1&currnumber gives it means we will increament our final ans
by 1
we will right shift current number till current number become 0.
follow code below.
code-


Advertiser

all right reserved @technicalkeeda.in. Powered by Blogger.