Bob has a strange counter. At the first second, , it displays the number . At each subsequent second, the number displayed by the counter decrements by .
Here is my code:
The counter counts down in cycles. In the second after the counter counts down to , the number becomes the initial number for that countdown cycle and then continues counting down from the new initial number in a new cycle. The diagram below shows the counter values for each time in the first three cycles:
Given a time, , find and print the value displayed by the counter at time .
Hints:
- The last 'time' value of cycle 1 is 3 which is 3*1=3*(2^1-1).
- The last 'time' value of cycle 2 is 9 which is 3*3=3*(2^2-1).
- The last 'time' value of cycle 3 is 21 which is 3*7=3*(2^3-1).
So, we will use a while loop and keep doubling n, which will represents 2^1, 2^2, 2^3 part. The loop stops when it reaches to a cycle whose max time is bigger than the real time t, then we know that it has reached the last cycle.
For example :
In the second cycle, time =7 has value=3, and the max time of the second cycle is 9. We find the relationship between them is 3 = 9 - 7 + 1, which is why we print (3 * (n - 1) - t + 1) to get the output of 3 when t is 7.
In the second cycle, time =7 has value=3, and the max time of the second cycle is 9. We find the relationship between them is 3 = 9 - 7 + 1, which is why we print (3 * (n - 1) - t + 1) to get the output of 3 when t is 7.
Here is my code:
#include <iostream>using namespace std;
int main(){
long long t,n=2;
cin >> t;
while(3*(n-1)<t)n*=2;
cout<<3*(n-1)-t+1<<endl;
return 0;
}
Question source:- hackerrank.com
Royal Rumble
ReplyDeleteRoyal Rumble Live
Royal Rumble 2018
WWE Royal Rumble
WWE Royal Rumble 2018
Royal Rumble
Royal Rumble 2018
Royal Rumble 2018 Live
WWE Royal Rumble
WWE Royal Rumble 2018
Pro Bowl
Pro Bowl 2018
Pro Bowl 2018
Pro Bowl 2018
Masters
ReplyDeleteMasters Live
Masters 2018
The Masters
2018 Masters
Masters Golf
Masters Tournament
Masters
Masters Live
Masters 2018
The Masters
2018 Masters
Masters Golf
Masters Tournament
Masters
ReplyDeleteMasters Live
The Masters
The Masters Live
Masters 2018
Masters Golf
Masters Tournament
Masters
Masters Live
The Masters
The Masters Live
The Masters 2018
Masters 2018
Masters Golf
The Masters
ReplyDeleteMasters 2018
Masters 2018 Live
2018 Masters
Masters Golf
Masters Tournament
The Masters
Masters 2018
Masters 2018 Live
Masters Golf
Masters Golf Live
Masters Golf 2018
Masters Tournament
Avengers Infinity War
ReplyDeleteAvengers Infinity War Movie
Avengers Infinity War Full Movie
Avengers Infinity War 2018
Infinity War
Infinity War Movie
Infinity War Full Movie
Avengers 3
Avengers 3 Movie
Avengers 3 Full Movie
Avengers 2018
NBA Finals
ReplyDeleteNBA Finals Live
NBA Finals 2018
NBA Finals
NBA Finals 2018
NBA Finals
NBA Finals Live
NBA Finals 2018
NBA Finals
NBA Finals 2018
UFC 229
ReplyDeleteUFC 229 Live
UFC 229 Live Stream
Watch UFC 229
229 UFC
UFC 229 Fight
UFC 229 Fight Card
McGregor vs Khabib
Khabib vs McGregor
McGregor vs Khabib Live
https://229ufc.net/
Khabib vs McGregor Live
McGregor vs Khabib Fight
Oscars
ReplyDeleteOscars Live
Oscars 2019
Oscars 2019 Live
91st Oscars
Academy Awards
Oscar Awards
Oscars
Oscars Live
Oscars 2019
Oscars 2019 Live
Academy Awards
Oscar Awards
canelo vs jacobs live streaming || canelo vs jacobs live|| canelo vs jacobs live stream 2019
ReplyDelete