Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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




SOlution -

If we do AND operation 
1&1=1
1&0=0
by using above property 
by 1
we will right shift current number till current number become 0.
follow code below.
question link-https://practice.geeksforgeeks.org/problems/set-bits0143/1
code-




This post first appeared on Technical Keeda, please read the originial post: here

Share the post

Count Set Bits GFG problems solution

×

Subscribe to Technical Keeda

Get updates delivered right to your inbox!

Thank you for your subscription

×