Help me in TWONUM

Hey Guys,I am getting TLE on a very basic and simple problem(feel bad for that)…I’ve tried the same logic of the code cramped into the main method hopin it would run faster but it took even more.
The Problem TWONUMS. TWONMS Problem - CodeChef
The original constraints are 1 sec and 50k bytes and the testcases are upto 100.

And somehow my code executes at 0.11 millisec at its best, and considering the codechef stress test(that is it will run the maximum number of testcases)…if i could reduce the execution time to 0.1 it would work.
And i couldnt figure what i can possibly reduce with the code.
Please do comment on my Coding prefrences or in anyway i could improve.
Any critics are highly encouraged.


    import java.util.Scanner;

class Operations extends Game
{
    //int num_alice,num_bob,turns;
    int result;


    void getData()
    {
        int num_alice = sc_ops.nextInt();
        int num_bob = sc_ops.nextInt();
        int turns = sc_ops.nextInt();
        gameplay(num_alice,num_bob,turns);
    }


    void gameplay(int num_alice,int num_bob,int turns)
    {
        for(int i=1;i<=turns;i++)
        {
            if(i%2==0)
            num_bob*=2;
            else
            num_alice*=2;
        }
        result(num_alice,num_bob);
    }

    void result(int num_alice,int num_bob)
    {
        //result=Math.max(num_alice,num_bob)/Math.min(num_alice,num_bob);
        result=(num_alice>num_bob)? (num_alice/num_bob):(num_bob/num_alice);
        System.out.println(result);
    }
}


class Game
{   static Scanner sc_ops= new Scanner(System.in);
    public static void main(String[] args)
    {   
        Operations obj = new Operations();
        int cases = sc_ops.nextInt();
        
        while(cases--!=0)
        {
            obj.getData();

        }
        
        
    }
}

Looking at your profile, it appears that you’ve found out how to solve this problem. So this thread can be closed now.

Ya, actually vijju helped me …which basically meant to change my code’s time from O(n) to O(1)…and the same thing helped me in another problem.
Lesson Learnt! :slight_smile: thanks vijju

1 Like