Skip to content

Commit 33a8492

Browse files
committed
Day-52 Heap Sort 2
1 parent 0d2c6bc commit 33a8492

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 72 |
8-
| Current Streak | 52 |
9-
| Longest Streak | 52 ( August 17, 2015 - October 7, 2015 ) |
7+
| Total Problems | 73 |
8+
| Current Streak | 53 |
9+
| Longest Streak | 53 ( August 17, 2015 - October 8, 2015 ) |
1010

1111
</center>
1212

@@ -64,6 +64,21 @@ Include contains single header implementation of data structures and some algori
6464
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
6565
| Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.|[swapSetOfBits.cpp](bit_manipulation/swapSetOfBits.cpp)|
6666
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)
67+
| Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations.
68+
69+
If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
70+
If N is a power of 2, reduce the counter by half of N.
71+
The resultant value is the new N which is again used for subsequent operations.
72+
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.
73+
74+
Given N, your task is to find the winner of the game.
75+
76+
If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.
77+
78+
Input Format
79+
The first line contains an integer T, the number of testcases.
80+
T lines follow. Each line contains N, the initial number set in the counter.
81+
|[counter_game.cpp](bit_manipulation/counter_game.cpp)|
6782

6883
### Cracking the coding interview problems
6984
| Problem | Solution |

bit_manipulation/counter_game.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
/*
3+
Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations.
4+
5+
If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
6+
If N is a power of 2, reduce the counter by half of N.
7+
The resultant value is the new N which is again used for subsequent operations.
8+
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.
9+
10+
Given N, your task is to find the winner of the game.
11+
If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.
12+
13+
Input Format
14+
The first line contains an integer T, the number of testcases.
15+
T lines follow. Each line contains N, the initial number set in the counter.
16+
17+
*/
18+
19+
20+
21+
#include <cmath>
22+
#include <cstdio>
23+
#include <vector>
24+
#include <iostream>
25+
#include <algorithm>
26+
using namespace std;
27+
28+
bool isPowerOf2(unsigned long long int x) {
29+
return ( x && ((x &(x-1)) == 0));
30+
}
31+
32+
unsigned long long int lowerPowerof2( unsigned long long int x) {
33+
if (x == 0) {
34+
return 0;
35+
}
36+
x--;
37+
x |= (x >> 1);
38+
x |= (x >> 2);
39+
x |= (x >> 4);
40+
x |= (x >> 8);
41+
x |= (x >> 16);
42+
x |= (x >> 32);
43+
return x - (x >> 1);
44+
}
45+
46+
std::string winner( bool win ) {
47+
if (win) {
48+
return std::string("Louise");
49+
} else {
50+
return std::string("Richard");
51+
}
52+
}
53+
54+
int main() {
55+
56+
int T;
57+
unsigned long long int N;
58+
cin >> T;
59+
60+
while(T) {
61+
cin >> N;
62+
if (N == 1) {
63+
std::cout << "Richard\n";
64+
continue;
65+
}
66+
bool win = false;
67+
while(N > 1) {
68+
if (isPowerOf2(N)) {
69+
N = N/2;
70+
} else {
71+
N = N - lowerPowerof2(N);
72+
}
73+
win = !win;
74+
}
75+
std::cout << winner(win) << std::endl;
76+
--T;
77+
}
78+
79+
return 0;
80+
}
81+
82+

0 commit comments

Comments
 (0)