Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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