How to develop an $O(N)$ algorithm solve the 2-sum problem?
Let $A$ be the sorted input array. Keep two pointers $l$ and $r$ that go through the elements in $A$. The pointer $l$ will go through the "left part" of $A$, that is the negative integers. The pointer $r$ does the same for the "right part", the positive integers. Below, I will outline a pseudocode solution and assume that $0 \notin A$ for minor simplicity. Omitted are also the checks for the cases where there are only positive or only negative integers in $A$.

COUNT-PAIRS(A[1..N]):
 l = index of the last negative integer in A
 r = index of the first positive integer in A
 count = 0;

 while(l &gt;= 0 and r &lt;= N)
   if(A[l] + A[r] == 0)
     ++count; ++right; --left; continue;

   if(A[r] &gt; -1 * A[l]) 
     --left;
   else 
     ++right;


It is obvious the algorithm takes $O(N)$ time.