title: Getting NZEC in Java for PARTSUM on SPOJ
body: <p>Array <code>T[]</code> has been used to store the sum of all possible contiguous combinations. And at each step, I am checking whether it is atleast <code>k</code> or not and that value is stored in the variable <code>val</code>. Below is the Java Code:</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Scanner;
  
  class Codechef {
      public static void main(String[] args) throws java.lang.Exception {
          Scanner sc = new Scanner(System.in);
          long t = sc.nextLong();
          while (t &gt; 0) {
              int n = sc.nextInt();
              long k = sc.nextLong();
              long p = sc.nextLong();
              long a[] = new long[n];
              long T[] = new long[((n * (n + 1)) / 2)];
              Arrays.fill(T, -1);
              long sum = 0, x = 0, val = p, f = 0;
              int i = 0;
              for (i = 0; i &lt; n; i++) {
                  a[i] = sc.nextLong();
                  sum = sum + a[i];
                  x = sum % p;
                  T[i] = x;
                  if (T[i] == k)
                      f = 1;
                  else if (T[i] &gt; k &amp;&amp; T[i] &lt; val)
                      val = T[i];
              }
              if (f != 1) {
                  for (int j = i - 1; j &gt; 0; j--) {
                      for (int m = 0; m &lt; j; m++) {
                          x = T[j] - T[m];
                          if (x &lt; 0)
                              x += p;
                          T[i] = x;
                          if (T[i] == k) {
                              f = 1;
                              break;
                          }
                          else if (T[i] &gt; k &amp;&amp; T[i] &lt; val)
                              val = T[i];
                          i++;
                      }
                      if (f == 1)
                          break;
                  }
              }
              if (f == 1)
                  System.out.println(k);
              else
                  System.out.println(val);
              t--;
          }
      }
  }
  </code></pre>
  
  <p>The above is the code for <a href="http://www.spoj.com/problems/PARTSUM/" rel="nofollow noreferrer">PARTSUM- Partial Sums</a> on SPOJ. What could be the possible error? </p>
  
  <p>Previously I was getting <strong>Runtime error NZEC</strong> on submit. Now that has been corrected and now I am getting <strong>TLE</strong>. Any optimization?</p>