Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Summary Ranges problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Summary Ranges problem solution, you are given a sorted unique integer array nums.

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:

  1. “a->b” if a != b
  2. “a” if a == b
Leetcode Summary Ranges problem solution

Leetcode Summary Ranges problem solution in Python.

class Solution(object):
def summaryRanges(self, nums):

    summary = []
    
    
    if len(nums) > 1:

        for i in range(len(nums)-1):

            if i == 0:
                start = nums[0]
                end = nums[0]

            if nums[i+1] == end+1:
                end = end+1

            else:
                if start!=end:
                    summary.append(str(start)+"->"+str(end))
                else:
                    summary.append(str(start))
                start = nums[i+1]
                end = nums[i+1]


        if start!=end:
            summary.append(str(start)+"->"+str(end))
        else:
            summary.append(str(start))
            
    
    if len(nums) == 1:
        summary.append(str(nums[0]))
    
    return summary

Summary Ranges problem solution in Java.

import java.util.*;

class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ranges = new ArrayList<>();
        int startI = 0;
        int endI = 0;
        for(int i=0; i<nums.length; i++) {
            startI = i;
            while(i < nums.length-1 && nums[i+1] == nums[i] + 1) {
                i++;
            }
            endI = i;
            
            if (startI == endI) {
                ranges.add(String.valueOf(nums[startI]));
            } else {
                ranges.add(nums[startI] + "->" + nums[endI]);
            }
        }
        return ranges;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = i; j < nums.size(); ++j) {
                if (j == nums.size() - 1 || (long long)nums[j + 1] - nums[j] > 1) {
                    string s = to_string(nums[i]);
                    if (j > i) {
                        s += "->" + to_string(nums[j]);
                    }
                    result.push_back(s);
                    i = j;
                    break;
                }
            }
        }
        return result;
    }
};

Problem solution in C.

char ** summaryRanges(int* nums, int numsSize, int* returnSize){
    uint i,start,capacity = 8;
    char **output;
    char *str;
    *returnSize = 0;
    if(!numsSize)
        return output;
    output = (char**)malloc(sizeof(char*)*capacity);
    for(i = 0,start = 0; i < numsSize; i++)
    {
        if(nums[i] - (i - start) != nums[start])
        {
            str = (char*)malloc(sizeof(char)*25);
            if(i-start == 1)
                sprintf(str,"%d",nums[start]);
            else
                sprintf(str,"%d->%d",nums[start],nums[i-1]);
            if(*returnSize == capacity)
            {
                capacity *=2;
                output = (char**)realloc(output,sizeof(char*)*capacity);
            }
            output[(*returnSize)++] = str;
            start = i;
        }
    }
    str = (char*)malloc(sizeof(char)*23);
    if(i-start == 1)
        sprintf(str,"%d",nums[start]);
    else
        sprintf(str,"%d->%d",nums[start],nums[i-1]);
    if(*returnSize == capacity)
    {
        capacity += 1;
        output = (char**)realloc(output,sizeof(char*)*capacity);
    }
    output[(*returnSize)++] = str;
    start = i;
    return output;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes