3

I am solving this question.

This is my code:

import java.io.IOException;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] t = new int[n];
        int count = 0;
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            if (t[i] % k == 0) {
                count++;
            }
        }
        System.out.println(count);

    }
}

But when I submit it, it get's timed out. Please help me optimize this to as much as is possible.

Example

Input:

7 3
1
51
966369
7
9
999996
11

Output:

4

They say :

You are expected to be able to process at least 2.5MB of input data per second at runtime.

Modified CODE

Thank you all...I modified my code and it worked...here it is....

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int k = Integer.parseInt(input[1]);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (Integer.parseInt(br.readLine()) % k == 0) {
                count++;
            }
        }
        System.out.println(count);
    }

regards

shahensha

shahensha
  • 2,051
  • 4
  • 29
  • 41
  • 1
    Why use `int[] t` if you are not using this array for any useful thing here? You could have use `int t` instead. – limc Feb 10 '11 at 17:51
  • This looks like input code for an ACM-ICPC problem, is this the entire "program"? From where is it timing out? an online judge? Can you give us some sample input? – Argote Feb 10 '11 at 17:53
  • yes that's the entire program...and they run the code on their servers....they have given us 8 seconds for this particular program....they say " You are expected to be able to process at least 2.5MB of input data per second at runtime." – shahensha Feb 10 '11 at 17:58
  • is the input formatted like that? or do you have an unknown amount of whitespace between each int? or is each number o a different line? – Argote Feb 10 '11 at 18:04
  • There is a space between n and k in the first line...and thereafter, each number is on a new line.... – shahensha Feb 10 '11 at 18:08
  • @Argote - I've formatted the Input and Output sections to make them clearer. – Andrzej Doyle Feb 10 '11 at 18:17
  • In this case you can use `BufferedReader` as I mentioned in the answer, get the first line, use `String.split` on the String and parse each element, then read each line and put it into an `Integer.parseInt` and use that in the modulo operation. – Argote Feb 10 '11 at 18:17
  • most efficient way ? – kta Feb 16 '15 at 11:56

4 Answers4

1

How about this?

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
    if (sc.nextInt() % k == 0) {
        count++;
    }
}
System.out.println(count);
limc
  • 39,366
  • 20
  • 100
  • 145
1

You may consider reading big chunks of input and then get the numbers from there.

Other change is, you may use Integer.parseInt() instead of Scanner.nextInt() although I don't know the details of each one, somethings tells me Scanner version performs a bit more computation to know if the input is correct. Another alternative is to convert the number yourself ( although Integer.parseInt should be fast enough )

Create a sample input, and measure your code, change a bit here and there and see what the difference is.

Measure, measure!

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

This could be slightly faster, based on limc's solution, BufferedReader should be faster still though.

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int count = 0;
        while (true) {
            try {
                if (sc.nextInt() % k == 0) {
                    count++;
                }
            } catch (NoSuchElementException e) {
                break;
            }
        }
        System.out.println(count);

    }
}
Argote
  • 2,155
  • 1
  • 15
  • 20
0

BufferedReader is supposed to be faster than Scanner. You will need to parse everything yourself though and depending on your implementation it could be worse.

Argote
  • 2,155
  • 1
  • 15
  • 20
  • Thanks a lot Argote! I implemented it with BufferedReader and without the array and it worked....Should I post the corrected code for others....??? – shahensha Feb 10 '11 at 18:12