Sorting +
Scanning
Learn the powerful two-phase pattern: sort first, then scan linearly to solve problems in \( O(n \log n) \) time that would otherwise require \( O(n^2) \).
What Is Sorting + Scanning?
A sorting + scanning pattern is a two-phase algorithmic strategy. First you sort the input data. Then you scan (iterate) through the sorted data in a single pass, exploiting the order to solve the problem efficiently.
This pattern appears constantly in competitive programming, technical interviews, and real-world systems: finding duplicates, detecting overlaps, computing closest pairs, and more.
The Two Phases
std::sort to arrange elements in non-decreasing order. Cost: \( O(n \log n) \).Why Does Sorting Help?
Sorting brings related elements next to each other. Instead of checking all \( \binom{n}{2} \) pairs, you only compare adjacent elements.
The Core Pattern in C++
Complexity Guarantee
Sort + Scan vs. Hash-Based
| Sort + Scan | Hash Set / Map | |
|---|---|---|
| Time complexity | \( O(n \log n) \) | \( O(n) \) average |
| Extra memory | \( O(1) \) extra (in-place sort) | \( O(n) \) |
| Order preserved? | No (array is reordered) | Yes (original order intact) |
| Works with custom comparators? | Yes, naturally | Needs custom hash |
| Best for | Interval problems, k-closest, ranges | Simple lookups, counting |
How to Apply the Pattern
std::sort. Call sort(v.begin(), v.end()) or pass a lambda for custom ordering.v[i] with v[i-1] or maintaining a running state variable.Example 1: Find Duplicates
Problem: Given an array of integers, determine if any value appears at least twice.
Example 2: Minimum Difference
Problem: Find the minimum absolute difference between any two elements in an array.
Example 3: Merge Overlapping Intervals
Problem: Given a list of intervals \( [a_i, b_i] \), merge all overlapping intervals.
Sort by start time, then scan: if the current interval overlaps the previous, merge them by extending the end. Otherwise, start a new group.
Merge Intervals: Code
Scan Variations
v[i] with v[i-1]. Used for duplicates, min gap, longest streak.Custom Comparators
Sometimes the default sort order is not enough. Use a lambda to define your own sort key:
Common Mistakes
sort() first. The adjacent-compare logic only works on sorted data.i = 0 and accessing v[i-1] causes an out-of-bounds read. Start at i = 1.std::sort requires strict weak ordering. Using <= instead of < leads to undefined behavior and crashes.Summary of Sort + Scan Problems
| Problem | Sort Key | Scan Type | Complexity |
|---|---|---|---|
| Find duplicates | Value | Adjacent == | \( O(n \log n) \) |
| Min difference | Value | Adjacent diff | \( O(n \log n) \) |
| Merge intervals | Start time | Running end | \( O(n \log n) \) |
| Two-sum (sorted) | Value | Two pointers | \( O(n \log n) \) |
| Max non-overlapping | End time | Greedy pick | \( O(n \log n) \) |
Question 1 of 5
What is the total time complexity of a sort + scan algorithm on an array of \( n \) elements?
Question 2 of 5
When scanning a sorted array for duplicates, what should you compare at each step?
v[i-1] is sufficient.Question 3 of 5
To merge overlapping intervals, what should you sort by?
Question 4 of 5
What happens if you use <= instead of < in a custom comparator passed to std::sort?
std::sort requires strict weak ordering. Using <= violates this requirement because equal elements would compare as "less than" each other, leading to undefined behavior.Question 5 of 5
You need to find the two closest values in an unsorted array. Which approach is most efficient?