Summary Ranges — Solution in Python, Java & C++
Summary Ranges solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays, asked at Indeed.
Summary Ranges is an easy coding problem on arrays, asked in Indeed interviews.
- Difficulty: Easy
- Topics: Arrays
- Asked at: Indeed
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given a sorted, unique integer array nums, return the smallest sorted list of ranges covering exactly the numbers in the array. Format each range as "a->b", or "a" when the range is a single number.
Example 1
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Example 2
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Constraints
0 <= nums.length <= 20-100 <= nums[i] <= 100Sorted, all values unique.
Hints for Summary Ranges
Hint 1
Walk forward while consecutive numbers differ by exactly 1.
Hint 2
Emit a->b for runs longer than one, else just a.
How to solve Summary Ranges
The array is already sorted with no duplicates, so a range is simply a maximal run of consecutive integers. Walk forward, extend the run while each next value is exactly one more than the last, then emit it in the required format and start the next run where this one stopped.
Approach
- Set
ito the start of a run. - Advance
jfromiwhilenums[j + 1] == nums[j] + 1. - If
i == jthe run is a single number — emit"a". - Otherwise emit
"a->b"using the first and last values of the run. - Continue from
j + 1until the array is exhausted.
Why it works
Sorted, distinct input means a run breaks exactly when the gap between neighbours exceeds one, so "maximal run" is a purely local test and one pass finds every boundary. Extending greedily to the longest possible run also produces the smallest list of ranges, because any shorter split would need an extra range to cover the same numbers.
Complexity
- Time —
O(n) - Space —
O(n)
Pitfalls
- A single number is formatted
"7", not"7->7". - An empty array returns an empty list; the loop must simply not run.
- Values can be negative, so build the strings from the numbers rather than assuming a fixed width.
- Compare
nums[j + 1] == nums[j] + 1, not just>— a jump of two starts a new range.
Reference solution
Python
from typing import List
def summaryRanges(nums: List[int]) -> List[str]:
out = []
i = 0
while i < len(nums):
j = i
while j + 1 < len(nums) and nums[j + 1] == nums[j] + 1:
j += 1
out.append(str(nums[i]) if i == j else f"{nums[i]}->{nums[j]}")
i = j + 1
return outJava
public static String[] summaryRanges(int[] nums) {
List<String> out = new ArrayList<>();
int i = 0;
while (i < nums.length) {
int j = i;
while (j + 1 < nums.length && nums[j + 1] == nums[j] + 1) j++;
if (i == j) out.add(String.valueOf(nums[i]));
else out.add(nums[i] + "->" + nums[j]);
i = j + 1;
}
return out.toArray(new String[0]);
}C++
vector<string> summaryRanges(vector<int>& nums) {
vector<string> out;
int n = (int) nums.size();
int i = 0;
while (i < n) {
int j = i;
while (j + 1 < n && nums[j + 1] == nums[j] + 1) j++;
if (i == j) out.push_back(to_string(nums[i]));
else out.push_back(to_string(nums[i]) + "->" + to_string(nums[j]));
i = j + 1;
}
return out;
}JavaScript
var summaryRanges = function(nums) {
const out = [];
let i = 0;
while (i < nums.length) {
let j = i;
while (j + 1 < nums.length && nums[j + 1] === nums[j] + 1) j++;
out.push(i === j ? String(nums[i]) : nums[i] + "->" + nums[j]);
i = j + 1;
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays