Check out example codes for "two pointer approach java". It will help you in understanding the concepts better.

Code Example 1

The algorithm basically uses the fact that the input array is 
sorted. We start the sum of extreme values (smallest and 
largest) and conditionally move both pointers. We move left 
pointer i when the sum of A[i] and A[j] is less than X. We 
do not miss any pair because the sum is already smaller than X. 
Same logic applies for right pointer j.

Learn ReactJs, React Native from akashmittal.com