I wrote a solution to HackerRank Strange Counter:
The counter counts down in cycles. At t = 1, the counter displays the number 3. At each subsequent second, the number displayed by the counter decrements by 1.
Each time the counter reaches 1, the number becomes 2× the initial number for that countdown cycle.
The sequence goes:
3, 2, 1, 6, 5, 4, 3, 2, 1, 12, 11, 10, 9, 8, 7, …
Print the value of the counter the time t, where 1 ≤ t ≤ 1012. For example, if t = 4, print
6.
My code works for inputs up to 100 million but when I enter number 1 billion or greater then I get TLE.
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
long long n;
cin >> n;
long long x = 3;
long long count = x;
for(long long i = 2; i <= n;i++)
{
count--;
if(i == n)
{
break;
}
else if(count == 1)
{ i++;
x = 2*x;
count = x;
}
}
cout << count;
return 0;
}