# Solution For Summary Ranges

Problem statement:
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.

Example:

Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output: [“2”, “4->49”, “51->74”, “76->99”]

Solution:
To solve this problem, we can traverse through the array and keep track of the current number and its previous number. If the difference between the current number and the previous number is greater than or equal to two, then we have a missing range between them. We can add this missing range to a list. At the end of the traversal, we need to check if there is any remaining range after the last element of the array, and if so, we can add that range to the list.

Algorithm:

1. Create an empty list to store the missing ranges.
2. Initialize a variable to keep track of the previous element of the array.
3. Traverse through the array using a for loop:
a. Check if the difference between the current element and the previous element is greater than or equal to two.
b. If so, then add the missing range in the format “prev+1->curr-1” to the list.
c. Update the previous element to the current element.
4. Check if there is any remaining range after the last element of the array.
a. If so, add the remaining range in the format “prev+1->upper” to the list.
5. Return the list of missing ranges.

Pseudocode:

“`
def summaryRanges(nums, lower, upper):
result = [] prev = lower-1 # initializing prev to lower-1 to handle the case where the first element of the array is lower.
for i in range(len(nums)):
if nums[i] – prev >= 2:
result.append(getRange(prev+1, nums[i]-1))
prev = nums[i]

``````# Check if there is any remaining range after the last element of the array.
if upper - prev >= 1:
result.append(getRange(prev+1, upper))
return result
``````

def getRange(start, end):
# returns the range in the format “start->end”
if start == end:
return str(start)
else:
return str(start) + “->” + str(end)
“`

Time complexity: O(n), where n is the length of the input array nums.
Space complexity: O(1), as we are only using constant amount of extra space to store the result list and a few variables.

## Step by Step Implementation For Summary Ranges

```Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

public List summaryRanges(int[] nums) {
List list = new ArrayList();
if(nums.length == 1){
return list;
}
for(int i = 0; i < nums.length; i++){
int a = nums[i];
while(i < nums.length - 1 && nums[i] + 1 == nums[i+1]){
i++;
}
if(a != nums[i]){
}else{
}
}
return list;
}```
```class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
# base case:
if len(nums) == 0:
return []
# initialize result list:
res = []
# pointer to keep track of current range start:
start = 0
# iterate through nums:
for i in range(1, len(nums) + 1):
# if i is at the end of the list or the current number is not equal to the next number - 1:
if i == len(nums) or nums[i] != nums[i - 1] + 1:
# add the current range to the result:
res.append(str(nums[start]) if start == i - 1 else str(nums[start]) + "->" + str(nums[i - 1]))
# update start pointer:
start = i
# return result:
return res```
```var summaryRanges = function(nums) {
};```
```vector summaryRanges(vector& nums) {
vector res;
int i = 0, n = nums.size();
while (i < n) {
int j = 1;
while (i + j < n && nums[i + j] - nums[i] == j) ++j;
res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
i += j;
}
return res;
}```
```public IList SummaryRanges(int[] nums) {
IList list = new List();
if (nums.Length == 0) return list;

int start = nums;
int end = nums;

for (int i = 1; i < nums.Length; i++) {
if (nums[i] == end + 1) {
end = nums[i];
}
else {
list.Add(start == end ? start.ToString() : start + "->" + end);
start = nums[i];
end = nums[i];
}
}

list.Add(start == end ? start.ToString() : start + "->" + end);

return list;
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]
{"ft_lang_label":"__label__en","ft_lang_prob":0.553754,"math_prob":0.9974328,"size":4062,"snap":"2023-40-2023-50","text_gpt3_token_len":1112,"char_repetition_ratio":0.1508132,"word_repetition_ratio":0.06793478,"special_character_ratio":0.33407187,"punctuation_ratio":0.17249417,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9989797,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-25T17:14:13Z\",\"WARC-Record-ID\":\"<urn:uuid:a0db92a4-228e-4cc4-b29c-fe863b5fad1d>\",\"Content-Length\":\"203293\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d1921119-6d1b-4e24-bb30-61dd7efa0ac9>\",\"WARC-Concurrent-To\":\"<urn:uuid:61d022d6-f0fb-4f7e-94c2-f662259cce57>\",\"WARC-IP-Address\":\"104.21.55.40\",\"WARC-Target-URI\":\"https://prepfortech.in/leetcode-solutions/summary-ranges/\",\"WARC-Payload-Digest\":\"sha1:AFWDGYY3ZDKSMJ3MIX5AUQKI3BJXW67S\",\"WARC-Block-Digest\":\"sha1:DVOIQSSBLULUY7NXHEMM7RDFMRKACFSV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233509023.57_warc_CC-MAIN-20230925151539-20230925181539-00423.warc.gz\"}"}