Summary Ranges
You are given a sorted unique integer array nums.
Return the smallest sorted list of ranges that covers all the numbers in the array exactly.
A range [a,b] is represented as:
• "a->b" if a != b
• "a" if a == b
Example 1
Input
6 0 1 2 4 5 7
Output
0->2 4->5 7
Explanation
The consecutive numbers 0, 1, and 2 form one range. The numbers 4 and 5 form another range, and 7 is a single-element range.
Example 2
Input
5 -5 -4 -2 -1 3
Output
-5->-4 -2->-1 3
Explanation
The numbers -5 and -4 are consecutive, so they form the range -5->-4. Similarly, -2 and -1 form the range -2->-1. The number 3 does not have any consecutive neighbors, so it is represented as a single number.
This example is different from the first two and demonstrates handling of negative numbers.
Constraints
Hints:
Hint 1
Start a new range from the current number and continue while the next number is consecutive.
Hint 2
If the start and end of the range are the same, add only one number. Otherwise, add the range in the format "start->end".
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 6, 2026
Last updated
Aug 9, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output