Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Summary Ranges problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes