Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

Say Goodbye to TLE: Master Two Pointers

Revision en1, by 3bdo_Khaled_X, 2026-09-01 19:56:52

What is the Two Pointers Technique:

uses two index variables to traverse a linear data structure—like an array, string, or linked list—to solve problems efficiently without nested loops.

Why Use it ?

Time Efficiency: It often reduces time complexity from O(n²) (using nested loops) down to linear time O(n).

Space Efficiency: It typically requires constant space O(1) because it only tracks a few indices.

Whats Patterns of 2 Pointers

Pattern 1 (Variable_Size)

1.1 Opposite Direction (Converging Pointers): One pointer starts at the beginning (left = 0) and the other at the end (right = n — 1). They move toward each other until they meet. ​Best Used For: Sorted arrays, pair sum problems (ex: Two Sum on sorted input), or checking palindromes.

Tags tutorial, two pointers, implementations, time limit, complexity

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English 3bdo_Khaled_X 2026-09-01 22:52:27 20
en3 English 3bdo_Khaled_X 2026-09-01 22:45:08 5664 Tiny change: 'order:\n\n​**Mov' -> 'order:\n\n\n​**Mov' (published)
en2 English 3bdo_Khaled_X 2026-09-01 20:09:17 630 Tiny change: 'romes.\n\nCan you ch' -> 'romes.\n\nProblem: Can you ch'
en1 English 3bdo_Khaled_X 2026-09-01 19:56:52 873 Initial revision (saved to drafts)