Skip to main content

Command Palette

Search for a command to run...

Summary Ranges

Published
2 min read
Summary Ranges
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a sorted unique integer array nums.

A range [a,b] is the set of all integers from a to b (inclusive).

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:

  • "a->b" if a != b

  • "a" if a == b

LeetCode Problem - 228

class Solution {
    public List<String> summaryRanges(int[] nums) {
        int n = nums.length;  // Get the length of the input array
        List<String> ans = new ArrayList<>();  // List to store the summary ranges
        if (n == 0) return ans;  // Return an empty list if the input is empty

        int start = 0;  // Initialize the start index of the current range

        // Loop through the array, using 'end' to explore potential ranges
        for (int end = 1; end <= n; end++) {
            // Check if we've reached the end of the array or if the current number
            // does not continue the consecutive range
            if (end == n || nums[end] != nums[end - 1] + 1) {
                // If the start and end indices are the same, it's a single number
                if (start == end - 1) {
                    ans.add(String.valueOf(nums[start]));  // Add the single number to the result
                } else {
                    // If there's a range, format it as "start->end"
                    ans.add(nums[start] + "->" + nums[end - 1]);
                }
                start = end;  // Move start to the current end index for the next range
            }
        }

        return ans;  // Return the list of summary ranges
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.