Sort by

recency

|

1005 Discussions

|

  • + 0 comments
    def strangeCounter(t):
        m=1
        i=1
        n=0
        while m<=t:
            m=3*i
            if t>n and t<=m:
                return ((m+1)-t)
            i+=i+1
            n=m
        
    
  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-strange-counter-problem-solution.html

  • + 0 comments

    python solution

    def strangeCounter(t):
        # Write your code here
    
        #base case of t in bucket 0
        if t<=3:
            return 3-t+1
        
        #determine what bucket t is in
        floor=3
        ceiling=9
        bucket=0
        for n in range(1,1000000):
            if t>floor and t<=ceiling:
                bucket=n
                break
            else:
                floor=ceiling
                ceiling=3*2**(n+1)+floor
        return 3*2**bucket-(t-floor-1)
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/Plsx1i8dqiI

    long strangeCounter(long t) {
        long last = 3, step = 3;
        while(t > last){
            step *= 2;
            last += step;
        }
        return last - t + 1;
    }
    
  • + 0 comments

    Python without loop

    def strangeCounter(t):
        # Write your code here
        r = math.ceil(math.log2(t/3+1)) # Calculate the number of current round
        sum_full = 3*(2**r-1) # Calculate the time when value reach to 1 at current round
        return sum_full - t + 1