We have a sorted array of unique integers, and we need to compress it into a list of ranges. Consecutive numbers get grouped into a single range like "a->b", while isolated numbers stand alone as "a".
Because the array is sorted and contains no duplicates, two values are consecutive integers exactly when one is one more than the other, and consecutive integers always sit next to each other in the array. So nums[i+1] == nums[i] + 1 means nums[i] and nums[i+1] belong to the same range. The first index where that relationship fails marks the end of the current range and the start of the next.
This is a grouping problem: scan through the array, find where each run of consecutive numbers starts and ends, and format each run as a string.
-2^31 <= nums[i] <= 2^31 - 1 - Values span the full 32-bit signed integer range. The expression nums[i] + 1 only runs when i + 1 < n, which means nums[i] < nums[i+1] <= 2^31 - 1, so nums[i] is at most 2^31 - 2 and the addition never overflows. Comparing endpoints with start == nums[i] is also safe. Avoid a length check like nums[i] - start, which would overflow when a range spans most of the integer axis.nums is sorted and unique - No need to sort or deduplicate. The ordering supports a single linear pass.Walk through the array, and each time a break in the consecutive sequence appears, record the range that ended at the previous element.
Remember the value where the current range begins. Then keep advancing as long as the next number is one more than the current. At a gap (or the end of the array), the range from the start value to the current value is complete. Format it and continue from the next element.
i = 0 as the starting index.i < n:start = nums[i] as the beginning of a new range.i + 1 < n and nums[i + 1] == nums[i] + 1, advance i.nums[i] is the end of the current range.start == nums[i], add "start" to the result. Otherwise, add "start->nums[i]".i to the next unprocessed element.i, so the total number of iterations across both loops is n.i, start). The output list does not count toward auxiliary space.The next approach keeps the same linear pass but removes the inner loop, processing each element with a single comparison against the previous one.
Approach 1 uses two nested loops: an outer loop that opens a range and an inner loop that consumes consecutive elements. The two loops can collapse into one.
Scan the array once with a single index. Hold the start value of the current range. At each element, compare it to the element before it. If the current element is one more than the previous one, the run is still going, so do nothing. If it is not, the previous element closed a range: format that range with the held start value as [start, nums[i-1]], then set the start value to the current element. After the loop, one range is still open (the final run), so format it with the last element as its end.
This avoids the manual index juggling of the inner while loop. Every element is touched once, and the only decision per element is whether it continues the current run or begins a new one.
start = nums[0].i from 1 to n-1:nums[i] != nums[i-1] + 1, the run ended at nums[i-1]. Append the range [start, nums[i-1]] (formatted as a single value when start == nums[i-1]), then set start = nums[i].[start, nums[n-1]] using the same formatting rule.start value and loop index. The output list is not counted as auxiliary space.