title: Input array shows exception in thread "main" java.lang.negativearraysizeexception
body: <p>It works fine on VS Code, but when I run it on the compiler required for our training exam ,it shows the above error. What could be the reason?</p>
  <p>What does it mean? I'm new to programming languages. So please ignore my mistake.</p>
  <pre><code>    import java.util.Scanner;
  
      public class sum {
      public static void main(String[] args) {
  
          // Write solution here
  
          int n = 0, sum = 0;
  
          Scanner sc = new Scanner(System.in);
  
          n = sc.nextInt();
  
          int arr[] = new int[n];
  
          for (int i = 0; i &lt; n; i++) {
  
              arr[i] = sc.nextInt();
  
              if (arr[0] == 0) {
  
                  System.out.println(&quot;No elements to sum&quot;);
  
                  break;
  
              }
  
              else if (arr[0] &lt; 0) {
  
                  System.out.println(&quot;Invalid number of elements&quot;);
  
                  break;
  
              }
  
              else {
  
                  sum = sum + arr[i];
  
              }
  
          }
  
          System.out.println(sum);
          sc.close();
      }
  }
  </code></pre>