Strange Counter HackerRank Solution in C, C++, Java, Python

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*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.

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*3=6. This is shown in the diagram in the problem statement.

Strange Counter HackerRank 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;
}

 

Strange Counter HackerRank Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    long long cnt = 3;
    long long x;
    cin >> x;
    
    while(x > cnt) {
        x -= cnt;
        cnt *= 2;
    }
    
    cout << cnt - x + 1 << endl;
    
    return 0;
}

 

Strange Counter HackerRank 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);
    }
}

 

Strange Counter HackerRank Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT

t = int(raw_input())

phase = 3
while t > phase:
    t -= phase
    phase *= 2

print phase - t + 1

 

Attempt Strange Counter HackerRank Solution

Link – https://www.hackerrank.com/challenges/strange-code/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/3d-surface-area-hackerrank-solution/

Leave a Comment