A SINGLE LINE LOGIC ALSO GIVING TLE FOR DCE05 question !!!!

the question link is

 http://www.codechef.com/problems/DCE05

my solution is below…!!!
package mat;  
import java.lang.Math;  
import java.util.Scanner;
public class Mat {
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc;
        sc = new Scanner(System.in);
        double i,j,k,n;
        long ans,t;
        t=sc.nextLong();
        while(t-->0)
        {
        n=sc.nextLong();
        ans=(long)Math.pow(2,Math.getExponent(n));
        System.out.println(ans);
        }
    }

}  

the above code results TLE… can anyone plzzzz help me!!!

Tht means the question has a better and efficient implementation. Let me give u a hint - Think bitwise!
I have assumed here that the inbuilt java function is not doing the same

1 Like

got AC for this method
ans=1;
while((n>>1)>0) : n=n>>1;ans=ans<<1;
print ans;

1 Like

First u may try replacing Scanner with BufferedReader because scanner is very slow.Also use BufferedWriter to print. May be faster I/O will help. If u get TLE even after these changes , then u have to make ur own algorithm.

2 Likes