Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Sort the array, then fix one element with an outer loop and use two converging pointers for the remaining pair. Track the sum closest to the target by comparing absolute differences. Adjust pointers based on whether the current sum is less than or greater than the target.
Sorting allows the two-pointer approach to systematically explore sums: moving left increases the sum, moving right decreases it, efficiently narrowing in on the closest possible sum.
Updated Feb 2026