Skip to content

Commit

Permalink
Day-52 Heap Sort 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Oct 8, 2015
1 parent 0d2c6bc commit 33a8492
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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

</center>

Expand Down Expand Up @@ -64,6 +64,21 @@ Include contains single header implementation of data structures and some algori
| How many bit flip operation would require to convert number A to B. | [countNumberOfBitFlips.cpp](bit_manipulation/countNumberOfBitFlips.cpp)|
| 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)|
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)
| 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.

If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
If N is a power of 2, reduce the counter by half of N.
The resultant value is the new N which is again used for subsequent operations.
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.

Given N, your task is to find the winner of the game.

If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.

Input Format
The first line contains an integer T, the number of testcases.
T lines follow. Each line contains N, the initial number set in the counter.
|[counter_game.cpp](bit_manipulation/counter_game.cpp)|

### Cracking the coding interview problems
| Problem | Solution |
Expand Down
82 changes: 82 additions & 0 deletions bit_manipulation/counter_game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

/*
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.
If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
If N is a power of 2, reduce the counter by half of N.
The resultant value is the new N which is again used for subsequent operations.
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.
Given N, your task is to find the winner of the game.
If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.
Input Format
The first line contains an integer T, the number of testcases.
T lines follow. Each line contains N, the initial number set in the counter.
*/



#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool isPowerOf2(unsigned long long int x) {
return ( x && ((x &(x-1)) == 0));
}

unsigned long long int lowerPowerof2( unsigned long long int x) {
if (x == 0) {
return 0;
}
x--;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
x |= (x >> 32);
return x - (x >> 1);
}

std::string winner( bool win ) {
if (win) {
return std::string("Louise");
} else {
return std::string("Richard");
}
}

int main() {

int T;
unsigned long long int N;
cin >> T;

while(T) {
cin >> N;
if (N == 1) {
std::cout << "Richard\n";
continue;
}
bool win = false;
while(N > 1) {
if (isPowerOf2(N)) {
N = N/2;
} else {
N = N - lowerPowerof2(N);
}
win = !win;
}
std::cout << winner(win) << std::endl;
--T;
}

return 0;
}


0 comments on commit 33a8492

Please sign in to comment.