Learnitweb

LeetCode Problem 228 Summary Ranges


1. Problem Summary

You are given a sorted integer array nums with no duplicates.
Your task is to summarize continuous ranges and return them as a list of strings.

The formatting rules are:

  • If a number stands alone (no consecutive continuation), represent it as: "x"
  • If a range is continuous from x to y, represent it as: "x->y"

Example interpretation:
For input:

[0, 1, 2, 4, 5, 7]

Output:

["0->2", "4->5", "7"]

2. Key Insights

Array is already sorted and unique

So we only need to detect breaks in continuity.

Consecutive numbers differ by exactly 1

A range continues as long as:

nums[i] == nums[i-1] + 1

Track start and end of each range

We maintain:

  • start = beginning of current range
  • iterate until a break occurs
  • close and record the range
  • start a new range

Ranges are emitted when:

  • a gap is detected
  • end of array is reached

Output format handling is essential

Two forms only:

  • single value
  • value followed by arrow

3. Approach

Linear scan with range tracking

Steps:

  1. If array is empty → return empty list.
  2. Initialize start = nums[0].
  3. Loop through array from index 1 onward:
    • If current number is not consecutive:
      • close previous range
      • append formatted output
      • reset start = nums[i]
  4. After loop ends:
    • close and record the final range
  5. Return result list.

All comparisons require only neighboring elements.


4. Time and Space Complexity

Time Complexity: O(N)

Single pass through the array.

Space Complexity: O(1) additional (excluding output)

Only tracking a few variables.


5. Java Solution

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        if (nums.length == 0) {
            return result;
        }

        int start = nums[0];

        for (int i = 1; i < nums.length; i++) {
            // Break in continuity detected
            if (nums[i] != nums[i - 1] + 1) {
                if (start == nums[i - 1]) {
                    result.add(String.valueOf(start));
                } else {
                    result.add(start + "->" + nums[i - 1]);
                }
                start = nums[i];
            }
        }

        // Add last pending range
        if (start == nums[nums.length - 1]) {
            result.add(String.valueOf(start));
        } else {
            result.add(start + "->" + nums[nums.length - 1]);
        }

        return result;
    }
}

6. Dry Run Examples

Example 1

Input:

nums = [0, 1, 2, 4, 5, 7]

Processing:

Range starts at 0
0,1,2 are consecutive → break at 4
Record: “0->2”

Range starts at 4
4,5 consecutive → break at 7
Record: “4->5”

Final number:
Record: “7”

Output:

["0->2", "4->5", "7"]

Example 2

Input:

nums = [1, 3, 5, 7]

All standalone numbers:

Output:

["1", "3", "5", "7"]

Example 3

Input:

nums = [-3, -2, -1, 0, 2, 3, 4]

Ranges:

  • “-3->0”
  • “2->4”

Output:

["-3->0", "2->4"]

Example 4 (single element)

Input:

nums = [9]

Output:

["9"]

Example 5 (empty array)

Input:

nums = []

Output:

[]

7. Why This Solution Works

  • Makes a single linear scan
  • Uses sorted and unique constraints effectively
  • Detects gaps with simple arithmetic
  • Produces formatted results precisely
  • Works for negative values
  • Works for large integer ranges
  • Minimal logic, no unnecessary data structures

8. Common Mistakes

  1. Treating unsorted arrays (problem guarantees sorting)
  2. Forgetting to append the final range
  3. Off-by-one comparisons
  4. Misformatting single-element ranges
  5. Using string concatenation inside loop inefficiently