Sort by

recency

|

1006 Discussions

|

  • + 0 comments

    import java.io.*;

    class Result {

    public static long strangeCounter(long t) {
        long cycleStart = 1;
        long cycleValue = 3;
    
        while (t > cycleStart + cycleValue - 1) {
            cycleStart += cycleValue;
            cycleValue *= 2;
        }
    
        return cycleValue - (t - cycleStart);
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        long t = Long.parseLong(bufferedReader.readLine().trim());
    
        long result = Result.strangeCounter(t);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 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;
    }