Strange Counter

Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Yeah I was also getting some errors while writing the code so I used the superiorpapers.com review to find my mistake. It allowed me to identify the mistake and then also correct it.

  • + 0 comments

    Test cases 4,5,6,8 are time out can anyone help me with this??

  • + 0 comments

    My solution public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long t =in.nextLong();
        long v=3,i=1,v1=3;
        while(i<=t)
            {
            if(v1==1)
                {
                i=i+1;
                v=v*2;
                v1=v;
            }
            else
                {
                i=(i+v-1);
                v1=1;
            }
        }
        if(i==t)
            {
            System.out.println(v1);
        }
        else
            {
            System.out.println(i-t+1);
        }
    }
    

    }

  • + 0 comments

    O(1) solution

    #include <stdio.h>
    #define POW(k) (((k)&(k-1)) == 0)
    #define asm __asm__
    
    int main() {
        long t, start;
        scanf("%li", &t);
        if((t+2) % 3 == 0 && POW((t+2)/3)) {
            printf("%li\n", t+2);
        } else {
            start = (t+2)/3;
            asm("movq %0, %%r8"::"r"(start));
            asm("bsr %r8, %rcx");
            asm("movq $0x01, %rax");
            asm("shl %cl, %rax");
            asm("movq %%rax, %0":"=r"(start));
            printf("%li\n", 6*start-2-t);
        }
        return 0;
    }
    
  • + 1 comment

    Simple C code:

    int i;
    long long int t,k=3;
    scanf("%lld",&t);
    while(k<t){
        t-=k;
        k*=2;
    }
    printf("%lld",k-t+1);
        return 0;