The Magic

QUESTION

Navi got a task at school to collect N stones. Each day he can collect only one stone. As N can be a very large number so it could take many days to complete the task, but then he remembers that his mother gave him a magic that can double anything (i.e if he has 2 stones, the magic will make them to 4 stones). \n\nNavi can use this magic any number of time on the collected stone on a particular day and add this to the previously collected stones. \n\nRemember that he wants exactly N stones and he can’t throw any stone. If he gets more than N stones then he gets 0 marks, of course he doesn’t want 0 marks. \n\nHelp him to collect exactly N stones in minimum number of days.\n\nInput\n\nFirst line of input will contain number of test cases (T). Then next T lines contains a single number N, which is number of stones Navi has to collect.\n\nOutput\n\nFor each test case, Print a single number which is the minimum number of days taken by Navi to complete the task.\n\nConstraints\n\n1 <= T <= 10^5\n\n0 <= N <= 10^9.

ANSWER

#include <stdio.h>

int ones(int n)
{
 int count = 0;
 while(n)
 {
  if(n&1)
   count++;
  n>>=1;
 }
 return count;
}

int main()
{
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
     scanf("%d",&n);
     printf("%d\n",ones(n));
    }
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro