LT01-Editorial

Problem Link

Loop Through

Author:shail121

Difficulty

EASY

Prerequisites

bitmasks

Problem Statement

Tabitha loved vintage items and she was extremely fond of collecting postcards.

She came across a vintage store which would deliver an extremely unique postcard to her everyday if she wrote articles about the store for their blog.

She readily agreed to it and was very excited to collect all those postcards in her memo box

Initially, the box is empty. Each morning, Tabitha receives one new postcard for her memo box on writing that day’s article and the total number of postcards double by the end of the day. If she wants to receive x postcards at the end, how many articles should she write?

Approach

Write down x into its binary form. If the ith least significant bit is 1 and x contains n bits, we put one postcard into this box in the morning of (n + 1 - i)thday. Then at the noon of the nth day, the box will contain x postcards. So the answer is the number of ones in the binary form of x.

Solution

#include

using namespace std;
int main()
{
long long int n,i,x=0;
scanf("%lld",&n);
while(n>=1){
if(n%2==1){
x++;
n–;
}
else{
n/=2;
}
}
printf("%d\n",x);
return 0;
}

Atleast editorials must be formatted well :slightly_smiling_face:.