title: Sum two enormous long in Java
body: <p>I wrote this method which search in the arrayList if there are two numbers which sum equals the variable <strong>elem</strong>. The problem is that the sum of the variable exceed the dimension of the long type. How can I write it?</p>
  
  <pre><code>public static boolean searchSum(ArrayList&lt;Long&gt; array, long elem) {
     int left = 0, right = array.size()-1;
     while (left&lt;right) {
       long n1=Long.valueOf(array.get(left));
       long n2=Long.valueOf(array.get(right));
       if ((n1+n2)==elem) return true;
       else if ((n1+n2)&lt;elem) left++;
       else right--;
     }
     return false;
   }
  </code></pre>