The two-pointer technique maintains two indices whose monotone movement avoids revisiting candidate pairs. To find two values summing to in an increasing array, start at its ends. If their sum is too small, move the left index right: every pair using the old left value is too small. If too large, move the right index left by the symmetric argument. Each step eliminates one index, so after sorting the scan takes time. The correctness depends on sorted order; blindly applying the rule to unsorted inputs can discard a valid pair.

Articles by others on the same topic (0)

There are currently no matching articles.