We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Strange Counter
  5. Discussions

Strange Counter

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 865 Discussions, By:

recency

Please Login in order to post a comment

  • hariprasaadsoun1
    2 weeks ago+ 0 comments

    Simple Javascript Solution:

    function strangeCounter(t) {
        let a = 3;
        let x = 1;
        let y = 3;
    
        while (y < t) {
            a = a * 2;
            x = y + 1;
            y = x + (a - 1);
        }
    
        return (a - (t-x))
    }
    
    0|
    Permalink
  • davidabbs_abbs1
    2 weeks ago+ 0 comments

    Java

    public static long strangeCounter(long t) {
            long maxSize = 3;
            long count = 0;
            while (count<t){
                    count+=maxSize;
                    maxSize*=2;
            }
            return (maxSize/2)-((maxSize/2)-((count-t)+1));
    }
    
    0|
    Permalink
  • madhureddy8801
    3 weeks ago+ 0 comments
    start_value=3
        ending_value =3
        while ending_value <t:
            start_value *=2
            ending_value +=start_value
        return ending_value-t+1
    
    0|
    Permalink
  • donopiyo
    3 weeks ago+ 0 comments

    Java

    public static long strangeCounter(long t) {
        // Write your code here
            //3*2^0 + 3 * 2^1 + 3*2^n
            long x = 0L;
            long i = 0L;
            long s = 0L;
            long k = 1L;
            while(x < t){
                x = (long)(3 * Math.pow(2,i));
                k += x;
                if(k > t) {
                    s = k - t;
                    break;
                } 
                i++;
            }
            return s;
        }
    
    0|
    Permalink
  • ArunMozhiDevan
    1 month ago+ 0 comments

    PY

    Simple code:

    def strangeCounter(t):
        start_value = 3
        while  start_value - 2 <= t:
            start_value *= 2
        return start_value - 2 - t
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy