You are given a sorted unique integer array nums.
A range [a,b] is the set of all integers from a to b (inclusive).
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
Each range [a,b] in the list should be output as:
"a->b" if a != b"a" if a == b
Example 1:
Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
Example 2:
Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
Constraints:
0 <= nums.length <= 20-231 <= nums[i] <= 231 - 1nums are unique.nums is sorted in ascending order.Problem Overview: You receive a sorted array of unique integers. The task is to compress consecutive values into readable ranges. For example, [0,1,2,4,5,7] becomes ["0->2","4->5","7"]. Each continuous sequence should be represented by its start and end.
Approach 1: Sequential Range Detection (Time: O(n), Space: O(1) excluding output)
This method scans the array from left to right and builds ranges as soon as a break in consecutiveness appears. Start by marking the first element as the beginning of a range. Continue iterating while the next value equals current + 1. When the sequence breaks, close the range and append it to the result. If the start and end are the same, output a single number; otherwise format it as start->end. The algorithm performs one linear pass, so the time complexity stays O(n) with constant auxiliary space.
This solution relies only on simple iteration over an array. It's easy to implement and works well because the input is already sorted and contains unique values. Every number is visited exactly once.
Approach 2: Optimal Pointer Movement (Time: O(n), Space: O(1) excluding output)
This approach uses a two-pointer scanning pattern. Maintain two indices: start marking the beginning of a range and i expanding forward. While nums[i] == nums[i-1] + 1, keep extending the range. Once the condition fails, finalize the range from start to i-1 and reset start = i. The two pointers effectively track the current window of consecutive values.
The logic is identical to detecting continuous segments in many array traversal problems. The technique resembles a sliding window without shrink operations, making it a common pattern in two pointers problems. Each element advances the pointer once, so the runtime remains O(n) and no additional memory structures are required.
The advantage of the pointer-based version is clarity in interviews. Interviewers often expect candidates to identify consecutive segments using pointer movement instead of nested loops. It also scales naturally to similar problems such as merging intervals or detecting streaks.
Recommended for interviews: Start by explaining the sequential detection idea since it directly models the problem statement. Then implement the pointer-based scan, which interviewers usually view as the cleanest solution. Both run in O(n) time, but the pointer formulation demonstrates stronger control over iteration patterns and familiarity with two-pointer techniques.
This approach involves detecting the start and end of each range by iterating through the array sequentially. You remember the start of a potential range and adjust the range's end as long as consecutive numbers are found. When a break in consecutiveness occurs, you fix the end of the current range and start a new one.
The C solution iterates through the sorted array. It uses a start pointer that marks the beginning of a range. If consecutive numbers appear, it keeps adjusting the endpoint of the range. When non-consecutiveness is found (or array ends), it forms a string for that range and starts a new one.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
This approach utilizes a two-pointer method where one pointer marks the beginning of a new range, and another pointer (or the loop index itself) expands the range as far as possible until the next number isn't consecutive. Once a sequence ends, if numbers are the same, it is a single-element range; otherwise, a range connecting two different numbers is formed.
The two-pointer approach in C keeps two indices. The starting pointer begins a new potential range. The other adjusts as long as numbers are consecutive. Once they diverge, it involves forming a possible range string or single number and returning when done.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
| Approach | Complexity |
|---|---|
| Sequential Range Detection | Time Complexity: O(n) |
| Optimal Pointer Movement | Time Complexity: O(n) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sequential Range Detection | O(n) | O(1) | Best for straightforward linear scans when the array is sorted and unique |
| Optimal Pointer Movement | O(n) | O(1) | Preferred interview solution using two-pointer style traversal |
Summary Ranges - Leetcode 228 - Arrays & Strings (Python) • Greg Hogg • 18,649 views views
Watch 9 more video solutions →Practice Summary Ranges with our built-in code editor and test cases.
Practice on FleetCode