CodeChef : Time Limit Exceeded by the following java code -


here link problem : - http://www.codechef.com/problems/intest/

following code : -

import java.util.*; import java.io.*;  class intest {     public static void main(string...s) {         string str = "";         try {             str = new bufferedreader(new inputstreamreader(system. in )).readline();         } catch (exception e) {             system.out.println(e);         }         string[] ar = str.split(" ");          int n = integer.parseint(ar[0]);         int k = integer.parseint(ar[1]);         int count = 0;         if (k <= 10000000) {             int[] t = new int[n];              (int = 0; <= n - 1; i++) {                 try {                     t[i] = integer.parseint(new bufferedreader(new inputstreamreader(system. in )).readline());                 } catch (exception e) {                     e.printstacktrace();                 }                 if (t[i] <= 1000000000) {                     if (t[i] % k == 0) count++;                  } else break;             }         }         system.out.println(count);      } } 

i changed scanner bufferedreader read data could'nt reduce time.

any how can reduce time. thanks.

the problem :

the purpose of problem verify whether method using read input data sufficiently fast handle problems branded enormous input/output warning. expected able process @ least 2.5mb of input data per second @ runtime.

input

the input begins 2 positive integers n k (n, k<=10^7). next n lines of input contain 1 positive integer ti, not greater 10^9, each.

output

write single integer output, denoting how many integers ti divisible k. example

input: 7 3

1

51

966369

7

9

999996

11

output:

4

object initialization costly(i.e. using new). should avoid as can). in case create scanner object once , reuse it.

for example

class intest {     public static void main(string...s) {         string str = "";          scanner input=new scanner(system.in);         try {             str = input.readline();         } catch (exception e) {             system.out.println(e);         }         string[] ar = str.split(" ");          int n = integer.parseint(ar[0]);         int k = integer.parseint(ar[1]);         int count = 0;         if (k <= 10000000) {             int[] t = new int[n];              (int = 0; <= n - 1; i++) {                 try {                     t[i] = integer.parseint(input.readline());                 } catch (exception e) {                     e.printstacktrace();                 }                 if (t[i] <= 1000000000) {                     if (t[i] % k == 0) count++;                  } else break;             }         }         system.out.println(count);      } } 

note: there still more optimization can given code. example using nextint instead of nextline convert integer. in addition can assume input, don't need check value time.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -