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.
Problem: Can you choose two indices i , j (i≠j) and ai+aj=K If you can choose to indices solving the equation print "YES" otherwise "NO"
Brute Force Approach (TLE-->O(N^2))
int n; long long x; cin >> n >> x;
vector<long long> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Brute Force: Check every pair (i, j) where i != j
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == x) {
cout << "YES\n";
return 0;
}
}
}
cout << "NO\n";


