Hello Programmers, In this post, you will learn how to solve HackerRank Strange Counter Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

HackerRank Strange Counter
Task
There is a strange counter. At the first second, it displays the number 3. Each second, the number displayed by decrements by 1 until it reaches 1. In next second, the timer resets to 2 x the initial number for the prior cycle and continues counting down. The diagram below shows the counter values for each time t in the first three cycles:
Find and print the value displayed by the counter at time t.
Function Description
Complete the strangeCounter function in the editor below.
strangeCounter has the following parameter(s):
- int t: an integer
Returns
- int: the value displayed at time t.
Input Format
A single integer, the value of t.
Constraints
- 1 <= t <= 1012
Subtask
- 1 <= t <= 105 for 60% of the maximum score
Sample Input
4
Sample Output
6
Explanation
Time t = 4 marks the beginning of the second cycle. It is double the number displayed at the beginning of the first cycle: 2 x 3 = 6. This is shown in the diagram in the problem statement.
HackerRank Strange Counter Solution
HackerRank Strange Counter Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { long long n,f=3; scanf("%lld",&n); while(n>f) n-=f,f*=2; printf("%lld\n",f-n+1); return 0; }
HackerRank Strange Counter Solution in Cpp
#include <fstream> #include <iostream> #include <string> #include <complex> #include <math.h> #include <set> #include <vector> #include <map> #include <queue> #include <stdio.h> #include <stack> #include <algorithm> #include <list> #include <ctime> #include <memory.h> #include <assert.h> #define y0 sdkfaslhagaklsldk #define y1 aasdfasdfasdf #define yn askfhwqriuperikldjk #define j1 assdgsdgasghsf #define tm sdfjahlfasfh #define lr asgasgash #define norm asdfasdgasdgsd #define eps 1e-9 #define M_PI 3.141592653589793 #define bs 1234567891 #define bsize 350 using namespace std; const int INF = 1e9; const int N = 200000; long long n; int main(){ //freopen("fabro.in","r",stdin); //freopen("fabro.out","w",stdout); //freopen("F:/in.txt", "r", stdin); //freopen("F:/output.txt", "w", stdout); ios_base::sync_with_stdio(0); //cin.tie(0); cin >> n; long long P = 3; while (n > P) { n -= P; P *= 2; } cout << P - n + 1 << endl; cin.get(); cin.get(); return 0; }
HackerRank Strange Counter Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); long curr = 3; while (t > curr) { t -= curr; curr *= 2; } System.out.println(curr-t+1); } }
HackerRank Strange Counter Solution in Python
t = input() start = 0 interval = 3 while t > start + interval: start += interval interval *= 2 offset = t - start print interval - offset + 1
HackerRank Strange Counter Solution using JavaScript
function processData(input) { let n = parseInt(input); let l2 = Math.ceil(Math.log2(n / 3 + 1)); let s = 3 * (Math.pow(2, l2 - 1)); let s2 = s - 2; let index = n - s2; //console.log(l2, s, index, s - index); console.log(s - index); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
HackerRank Strange Counter Solution in Scala
object Solution { def main(args: Array[String]) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ io.Source.stdin.getLines.map{ _.toLong }.foreach { t => @scala.annotation.tailrec def helper( x: Long, v: Long ): Long = { if ( x <= v ) { (v - x) + 1 } else { helper( x - v, v * 2 ) } } println( helper( t, 3 ) ) } } }
HackerRank Strange Counter Solution in Pascal
const inp = 'input.txt'; out = 'output.txt'; var n, time, val : int64; procedure init; begin readln(n); end; procedure solve; var L : int64; begin time := 1; val := 3; while (time + val <= n) do begin time := time + val; val := val * 2; end; L := n - time; val := val - L; writeln(val); end; begin //assign(input, inp); reset(Input); //assign(output, out); rewrite(output); init; solve; end.
Disclaimer: This problem (Strange Counter) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.