In this HackerRank Strange Counter problem, There is a strange counter. In the first second, it displays the number 3. Each second, the number displayed by decrements by 1 until it reaches 1. In the next second, the timer resets to 2 x the initial number for the prior cycle and continues counting down. you need to find and print the value displayed by the counter at time t.

HackerRank Strange Counter problem solution


Problem solution in Python programming.

t = int(input())
n = 0
count = 0
while n < t:
    n += 3*(2**count)
    count += 1
print(n-t+1)


Problem solution in Java Programming.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class A {
	
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		PrintWriter out = new PrintWriter(System.out);
		long t = sc.nextLong();
		long cur = 0;
		long nxt = 3;
		while (cur < t) {
			cur += nxt;
			nxt <<= 1;
		}
		long val = 1;
		long dif = cur - t;
		val += dif;
		
		out.print(val);
		out.flush();
		out.close();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++)
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
}


Problem solution in C++ programming.

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

int main() {
	long long t;
	while(~scanf("%lld", &t)) {
		-- t;
		ll x = 3;
		while(t >= x) {
			t -= x;
			x *= 2;
		}
		printf("%lld\n", x - t);
	}
	return 0;
}


Problem solution in C programming.

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

int main() {
    long long int t;
    scanf("%lld",&t);
    long long int i=0,p=2,v;
    for(;;i++)
        {
         v=3*(p-1);
        if(v>=t)
            break;
        p=p*2;
    }
    long long int k=(p/2)*3;
    for(long long int j=3*(p/2-1)+1;j<=t;j++)
        {
        k--;
    }
    printf("%lld",k+1);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var t = parseInt(readLine());
//  3*(2^(k-1) - 1) < t <= 3*(2^k - 1)
    
    function lower( k ) {
        return 3 * ( Math.pow( 2, (k - 1) ) - 1 );
    }
    function upper( k ) {
        return 3 * ( Math.pow( 2, k ) - 1 );
    }
    function isInRange( k ) {
        return upper(k) >= t && lower(k) < t;
    }
    
    let k = 1;
    while( !isInRange(k) ) {
        k++;
    }
    console.log( upper(k) - t + 1 );
    
}