From 33a84924188e19b999ada7bc997ec895076ed8e3 Mon Sep 17 00:00:00 2001 From: mandliya Date: Thu, 8 Oct 2015 00:02:01 -0400 Subject: [PATCH] Day-52 Heap Sort 2 --- README.md | 21 ++++++-- bit_manipulation/counter_game.cpp | 82 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 bit_manipulation/counter_game.cpp diff --git a/README.md b/README.md index 0055631..8ee116b 100644 --- a/README.md +++ b/README.md @@ -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 ) | @@ -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 | diff --git a/bit_manipulation/counter_game.cpp b/bit_manipulation/counter_game.cpp new file mode 100644 index 0000000..85454ca --- /dev/null +++ b/bit_manipulation/counter_game.cpp @@ -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 +#include +#include +#include +#include +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; +} + +