-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncredibleHulk.cpp
More file actions
executable file
·72 lines (58 loc) · 1.59 KB
/
IncredibleHulk.cpp
File metadata and controls
executable file
·72 lines (58 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
The Planet Earth is under a threat from the aliens of the outer space and the Marvel Avengers team is busy fighting against them.
Meanwhile, The Incredible Hulk has to defeat an enemy who is N steps above the level where he is standing (assume it as the 0th level).
Hulk, because of his incredible jumping ability can take jumps in power of 2.
In order to defeat the enemy as quickly as possible he has to reach the Nth step in minimum moves possible.
Help Hulk to find the same and contribute in saving the Mother Earth.
Input Format
The first line contains the number of test cases T. T test cases follow: The first line of each test case contains a number N.
Constraints
1 <= T <= 10
1 <= N <= 10^5
Output Format
Output T lines, containing the minimum number of moves required by Hulk to reach the Nth step
Sample Input
3
3
4
5
Sample Output
2
1
2
Explanation
Let total steps is n, find the nearest integer which is of power 2 and less then n.
let it would be k. now remaining steps to cover is n-k and result = 1 + min steps for (n-k) remaining steps.
*/
#include <iostream>
using namespace std;
int countBits(int n){
int a = 0;
while(n > 0){
a = a + (n & 1);
n = n >> 1;
}
return a;
}
int countBitsFast(int n){
int a = 0;
while(n > 0){
n = n & (n-1);
a++;
}
return a;
}
int main(){
int t;
cin >> t;
int n;
int arr[100];
for(int i = 0; i < t; i++){
cin >> n;
arr[i] = __builtin_popcount(n);
}
for(int i = 0; i < t; i++){
cout << arr[i] << endl;
}
return 0;
}