PROBLEM LINK:
Setter: Krunal Mathukiya
Tester: Jaydeep Mulani
Editorialist: Malhar Patel
DIFFICULTY:
Simple
PREREQUISITES:
Basic observations, binary representation.
PROBLEM:
The idea behind the problem boils down to following : Given a number N determine the number of set bits in its binary representation.
EXPLANATION
Here you just need to find the binary representation of the given number. For example N=10, then its binary representation is 1001. So the output for the same is 2.
You can use inbuilt function in CPP, Python etc. to find the answer in one step.
TIME COMPLEXITY
Time complexity is O(logN).
SOLUTIONS:
CPP Solution
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define llu unsigned long long int
#define fo(i,n) for(int i=0;i<n;i++)
#define eb emplace_back
#define M 1000000007
#define vi vector
#define vlli vector
#define pi pair<int,int>
#define mp make_pair
#define mapi map<int,int>
ll max(ll x,ll y)
{
if(x>y)
return x;
return y;
}
ll min(ll x,ll y)
{
if(x<y)
return x;
return y;
}
void yes()
{
cout << “YES” << endl;
}
void no()
{
cout << “NO” << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//cin.ignore(numeric_limits::max(),‘\n’);
//const unsigned int M=1000000007;
int test;
cin >> test;
//test=1;
while(test–)
{
ll n;
cin >> n;
cout << setbit(n) << endl;
}
return 0;
}
Feel free to Share your approach, if you want to. (even if its same ) . Suggestions are welcomed as always had been.