• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

The Magic HackerEarth Nirvana solutions Hiring Challenge

February 25, 2015 by Dhaval Dave

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). Navi can use this magic any number of time on the collected stone on a particular day and add this to the previously collected stones. Remember 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. Help him to collect exactly N stones in minimum number of days.

Input

First 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.

Output

For each test case, Print a single number which is the minimum number of days taken by Navi to complete the task.

Constraints

1 <= T <= 10^5
0 <= N <= 10^9

Sample Input (Plaintext Link)

2
1
3
Sample Output (Plaintext Link)
1
2
Explanation : 
When N = 1, in 1 day he can collect stone.
When N = 3, on 1st day he collect 1 stone, make it double. so now he has 2.
On day 2, he can collect one , so total now he has collected 3 stones in 2 days.
 
Code Solution : 
 
#include <stdio.h>

int main(){
int T,N,stone,remaining,day;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
day=1;
remaining=N;
    while(1)
    {
        if(remaining==1) 
        break;
        if(remaining==0)
        {day--; break;}
        stone=1;
        while( (stone*2) <= remaining){
            stone = 2*stone;
        }
    remaining = remaining - stone;
    day++;
    }
printf("%dn",day);
}

return 0;
}

Similar Articles

Filed Under: Hacker Earth Questions, Interview Questions, problem Tagged With: Mathematical

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Wrong Directions given find minimum moves so that he can reach to the destination

Number of Islands BFS/DFS

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Binary Tree in Java

C Program for TAIL command of UNIX

Handle duplicates in Binary Search Tree

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Templates in C++

Printing Longest Common Subsequence

VMWare Openings

Generate largest number arranging a no. of given non negative integer numbers

Find the number ABCD such that when multipled by 4 gives DCBA.

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

C++ OOPs Part2

Given Set of words or A String find whether chain is possible from these words or not

Daughter’s Age VeryGood Puzzle

Find min element in Sorted Rotated Array (Without Duplicates)

Maximum size of square sub matrix with all 1’s in a binary matrix

Introduction To Number Theory ( Part 1 )

K’th Largest Element in BST when modification to BST is not allowed

25 horses 5 tracks Find 3 fastest puzzle

TicTacToe Game As Asked in Flipkart

Get Minimum element in O(1) from input numbers or Stack

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

1014 Practice Question of New GRE – Princeton

Cisco Hiring Event 21st – 22nd Feb 2015

SAP Off Campus Hiring_ March 2015 Computer Skills

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Flipkart Set 1 On Campus with Answers

Length of the longest substring without repeating characters

Copyright © 2025 · Genesis Framework · WordPress · Log in