1

I was trying to solve classic count sort problem. my o/p is right, but the time limit exceeded.How can I optimize the below code. I'm under memory limit.

Time Limit: 5 sec Source Limit: 50000 Bytes

class TurboStart {
    static int integerArray[] = new int[1000001];

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        int  i, j;
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        j = Integer.parseInt(reader.readLine());

        while (j-- > 0) {
            integerArray[Integer.parseInt(reader.readLine())]++;
        }

        for (i = 0; i < 1000001; i++) {
            while (integerArray[i]-- > 0) {
                System.out.println(i);
            }

        }
    }
}
4
  • You might be better off on CodeReview.SE Commented Nov 12, 2014 at 10:02
  • What input arguments are you passing to TurboStart? Commented Nov 12, 2014 at 10:02
  • take a look at here ideone.com/KHfN0y Commented Nov 12, 2014 at 10:03
  • You might want to check your output loop, too: for frequent values, you are not only accessing the count array, but converting the count to digits over and again. Commented Nov 12, 2014 at 10:18

1 Answer 1

1

Don't use System.out.println but some smarter way of writing the output(BufferedWriter?). Your code for the sort is good so the bottleneck should be the I/O(which is often problem with Java on programming competitions).

Sign up to request clarification or add additional context in comments.

1 Comment

Work like a charm thanks a ton @Ivaylo Strandjev its was I/O problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.