×

# [closed] Help me in TWONUM

 0 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. https://www.codechef.com/problems/TWONMS 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(); } } }  asked 17 Jul '17, 14:58 80●6 accept rate: 12% 15.4k●1●20●66 also i have tried most trivial changes as well..still it runs at 0.11 millisec at best! (17 Jul '17, 15:01) Looking at your profile, it appears that you've found out how to solve this problem. So this thread can be closed now. (17 Jul '17, 23:30) 1 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! :) thanks vijju (20 Jul '17, 22:11)

### The question has been closed for the following reason "Other" by vijju123 18 Jul '17, 00:12

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,718
×720
×123

question asked: 17 Jul '17, 14:58

question was seen: 257 times

last updated: 20 Jul '17, 22:11