We can improve the time complexity of some solutions from quadratic to linear using the two-pointer approach

Two-Pointer Approach

To solve problems like Two Sums, Product Except Self, or Contains Duplicate we have to access multiple values at the same time from a sequential data structure (for example, a Linked List or an Array). The initial instinct while solving these problems is to use nested loops where each layer of the loop will maintain a different iterator on the data structure. But this approach does not scale well with the size of input as a single order of nested loops has $O(n^2)$ worst-case time complexity....

January 23, 2024 · 7 min · Avnish