> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/163-missing-ranges.md).

# 163 Missing Ranges

Given a sorted integer array where **the range of elements are in the inclusive range \[lower,upper]**, return its missing ranges.

For example, given`[0, 1, 3, 50, 75]`,lower= 0 and upper= 99, return`["2", "4->49", "51->74", "76->99"].`

```cpp
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) 
{
    vector<string> range_gaps;
    int low, high;

    // beginning range
    if (nums[0] - 1 == lower)
        range_gaps.push_back(to_string(lower));
    else if (lower < *nums.begin() - 2)
        range_gaps.push_back(to_string(lower) + "->" + to_string(nums[0] - 1));

    // inclusive range
    for (int i = 0; i < nums.size() - 1 && nums.at(i) >= lower && nums.at(i) <= upper; i++) {
        low = i;
        high = i + 1;

        if (nums[low] + 1 == nums[high]) continue;
        else if (nums[low] + 1 == nums[high] - 1) 
            range_gaps.push_back(to_string(nums[low] + 1));
        else 
            range_gaps.push_back(to_string(nums[low] + 1) + "->" + to_string(nums[high] - 1));
    }

    // ending range
    if (upper > *nums.rbegin() + 2) 
        range_gaps.push_back(to_string(*nums.rbegin() + 1) + "->" + to_string(upper));
    else if (*nums.rbegin() + 1 == upper - 1)
        range_gaps.push_back(to_string(*nums.rbegin() + 1));

    return range_gaps;
}

int main()
{
    vector<int> test = { 0,1,3,50,75 };
    vector<string> elements = findMissingRanges(test, 0, 99);
    print(elements);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/163-missing-ranges.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
