INTEST ,Java: getting TLE with Byte Array and InputStreamReader

Hi guys,
i tried to improve total execution time by using a faster method than BufferedReader and BufferedInputStream(both of which were accepted) and got time limit exceeded instead.
the code is similar to that of ‘martins’ submission.

import java.io.IOException;
import java.io.PrintWriter; 
public class Main { 
    public static void main(String[] args) throws IOException {        
        PrintWriter pw = new PrintWriter(System.out,true);
        byte[] arr = new byte[8192];
        int cread,n=0,k=0;
        while((cread = System.in.read())!=32)
        n=n*10+cread-48;
        while((cread=System.in.read())!=10)
            k=k*10+cread-48;
        
        //pw.println(n+ " " +k);
        int count=0;
        while((cread=System.in.read(arr,0,8192))>-1)
        {
            int i=0;
            byte b;
            while(i<cread)
            {
                int num=0;
                b=arr[i];
                while(b!=10)
                    num=num*10+b-48;
                if(num%k==0)
                    count++;                
            i++;
            }
        }
             pw.println(count);
    }
}

even the use of byte array is not effective.Plz help.

Did you test your code on your PC? I think, you didn’t!

What this part of code is doing?

b=arr[i];
while(b!=10)
    num=num*10+b-48;

Small hint for you, instead of constants 32, 10, 48 you can use chars to make your code easier to read - ' ', '\n', '0'

the constants are
32 for whitespace
10 for newline
and 48 to get integer values from ascii.
This part is converting the number in the the input stream into an integer;
eg
numarr (newline) numarr (newline) numarr (newline)…
here each byte array subpart b/w the new line feeds is converted into an integer each time.

You misunderstood both parts

  1. I know that ' ' char is converted to 32 int, but it is easier to write ' ' than remember or lookup int value for it, isn’t it?
  2. maybe you wanted the code will do what you wrote, but it’s not doing it, how could it? while ( b != 10 ) num = num * 10 + b - 48 is infinite loop ! And that’s the reason for TLE and not that it’s not effective…

oh,got it, thanks.