Editorial for virat_noob, https://www.codechef.com/BEST2021/problems/SACH12

Practice

Author: Anurag dubey
Tester: anurag dubey
Editorialist: anurag dubey

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

We have to just find out number of set bit present in given number

QUICK EXPLANATION:

You have given a just number we have to first find out binary equivalent to this number and in that find out number of 1’s present in it and then print it .

EXPLANATION:

Since we have given a number we have to first convert it into binary equivalent to it suppose a number is 5 its binary equivalent to this number is 101 since there is two set bit present in it, we will first store it in array then itrate it if we find 1 we will increase count and later we will print count that is number of set bit present in it .

SOLUTIONS:

Setter's Solution

//virat_noob
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n+1];
// cout<<“h”;
int i;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
int cnt=0;
for( i=i-1;i>=0;i–)
{
if(a[i]==1)
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}

Tester's Solution

//virat_noob
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n+1];
// cout<<“h”;
int i;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
int cnt=0;
for( i=i-1;i>=0;i–)
{
if(a[i]==1)
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}

Editorialist's Solution

//virat_noob
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n+1];
// cout<<“h”;
int i;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
int cnt=0;
for( i=i-1;i>=0;i–)
{
if(a[i]==1)
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}