HackerRank Strange Counter Solution

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.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Strange Counter Solution
HackerRank Strange Counter Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Strange Counter


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


  • int: the value displayed at time t.

Input Format

A single integer, the value of t.


  • 1 <= t <= 1012


  • 1 <= t <= 105 for 60% of the maximum score

Sample Input


Sample Output



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

Strange Counter Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    long long n,f=3;
    while(n>f) n-=f,f*=2;
    return 0;

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("F:/in.txt", "r", stdin);
	//freopen("F:/output.txt", "w", stdout);

	cin >> n;
	long long P = 3;
	while (n > P)
		n -= P;
		P *= 2;
	cout << P - n + 1 << endl;

	cin.get(); cin.get();
	return 0;

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;

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

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);

_input = "";
process.stdin.on("data", function (input) {
    _input += input;

process.stdin.on("end", function () {

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 => 
    def helper( x: Long, v: Long ): Long = {
      if ( x <= v ) {
        (v - x) + 1
      } else {
        helper( x - v, v * 2 )
    println( helper( t, 3 ) )

Strange Counter Solution in Pascal

        inp = 'input.txt';
        out = 'output.txt';
        n, time, val : int64;

procedure       init;

procedure       solve;
        var L : int64;
                time := 1; val := 3;
                while (time + val <= n) do
                                time := time + val;
                                val := val * 2;
                L := n - time;
                val := val - L;

        //assign(input, inp); reset(Input);
        //assign(output, out); rewrite(output);

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.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment