My code is fine and giving required output but it is not being compiled on server, any help would be appreciated.

public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int N = -1; while(N<=0){ N = Integer.parseInt(reader.readLine()); } int m = 0; int a[] = new int[N+1]; for (int i=0; i<N; i++) { a[i] = Integer.parseInt(reader.readLine()); } m = a[0]; for(int j=0; j<N; j++){ if (m < a[j+1]){ m = a[j+1]; } } //add your code here System.out.println(m); }

asked 28 Jul '15, 13:15

Asad's gravatar image

Asad
11
accept rate: 0%


What if all input numbers are negative? According to your code output will be zero instead of maximum number among those you have typed in.

Try to initialize m in a different way. What is the minimum number of type int?

link

answered 30 Mar '16, 07:55

ILIA%20KISIL's gravatar image

ILIA KISIL
122
accept rate: 0%

public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int maximum = -12700000;

        int num = Integer.parseInt(reader.readLine());
        for(int i = 0; i < num; i++){
            int j = Integer.parseInt(reader.readLine());
            if(j > maximum) maximum = j;
        }
        System.out.println(maximum);
    }
}
link

answered 18 May, 17:39

Igor's gravatar image

Igor
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×30
×21
×3

Asked: 28 Jul '15, 13:15

Seen: 2,281 times

Last updated: 18 May, 17:39