The complexity is often clear from the loops of an algorithm, but sometimes this simple analysis is too pessimistic. Amortized analysis estimates the total time of all operations during the algorithm instead of the time of a single operation.
In the two pointers method two pointers go through the array, each only in one direction.
Subarray sum
Given n positive integers and x, find a subarray with the sum x. In the array below the subarray 2, 5, 1 has the sum 8. The pointers mark the first and the last element of a subarray. At each turn the left pointer moves one step right, and the right pointer moves right as long as the sum stays at most x.
| 1 | 3 | 2 | 5 | 1 | 1 | 2 | 3 |
In a single turn the right pointer may move many steps, but in total it moves at most n steps, because it only moves right. So the whole algorithm is O(n).
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &v : a) cin >> v; // positive numbers
int right = 0, sum = 0; // the subarray is a[left..right-1]
for (int left = 0; left < n; left++) {
while (right < n && sum + a[right] <= x) {
sum += a[right];
right++;
}
if (sum == x) {
cout << "found: positions " << left << ".." << right - 1 << "\n";
return 0;
}
sum -= a[left];
}
cout << "not found" << "\n";
}
8 8 1 3 2 5 1 1 2 3
Output
found: positions 2..4
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &v : a) cin >> v;
int right = 0, sum = 0, found = -1;
for (int left = 0; left < n; left++) {
while (right < n && sum + a[right] <= x) sum += a[right++];
if (sum == x && found == -1) {
found = left;
cout << left << ".." << right - 1 << "\n";
}
sum -= a[left];
}
if (found == -1) cout << "not found" << "\n";
}
8 8 1 3 2 5 1 1 2 3
2SUM problem
Find two array values whose sum is x. Sort the array; the left pointer starts at the first value and moves right one step per turn, and the right pointer starts at the last value and moves left while the sum is larger than x. For [1, 4, 5, 6, 7, 9, 9, 10] and x = 12 the pointers find 5 + 7. Sorting is O(n log n) and the pointers move O(n) steps. (The same complexity is reached with n binary searches.)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &v : a) cin >> v;
sort(a.begin(), a.end());
int right = n - 1;
for (int left = 0; left < right; left++) {
while (right > left + 1 && a[left] + a[right] > x) right--;
if (a[left] + a[right] == x) {
cout << a[left] << " + " << a[right] << " = " << x << "\n";
return 0;
}
}
cout << "no solution" << "\n";
}
8 12 1 4 5 6 7 9 9 10
Output
5 + 7 = 12
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &v : a) cin >> v; // sorted
int right = n - 1;
bool found = false;
for (int left = 0; left < n - 1; left++) {
while (right > left + 1 && a[left] + a[right] > x) right--;
if (!found && left < right && a[left] + a[right] == x) {
cout << a[left] << " + " << a[right] << "\n";
found = true;
}
}
if (!found) cout << "no solution" << "\n";
}
8 12 1 4 5 6 7 9 9 10
Based on: Antti Laaksonen, Competitive Programmer's Handbook, section 8.1 (CC BY-NC-SA 4.0). Changed: the text is shortened and adapted, the code fragments are complete programs, and the diagrams, tasks and quizzes are new. Adaptation: Boban Blagojević, CC BY-NC-SA 4.0.