Problem intuition
The sorted order is the whole unlock here. If the current sum is too small, only the left pointer can help. If it is too large, only the right pointer can help.
That monotonic movement makes the two-pointer solution both correct and optimal after you move past the brute-force baseline.
Java solution ladder
The solutions below are ordered from least optimal to most optimal, so you can see the improvement path instead of only the final answer.