> 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/216_combination_sum_iii.md).

# 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

```
Input: k = 3, n = 7
Output:
[[1,2,4]]
```

Example 2:

```
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
```

* Very icky, brute force approach of finding all the valid combinations and testing them.

```cpp
vector<int> people;
vector<int> combination;
vector<vector<int>> final_combs;

void pretty_print(const vector<int>& v) {
    final_combs.push_back(v);
    //static int count = 0;
    //cout << "combination no " << (++count) << ": [ ";
    //for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; }
    //cout << "] " << endl;
    //pause();
}

void go(int offset, int k) {
    if (k == 0) {
        pretty_print(combination);
        return;
    }
    for (int i = offset; i <= people.size() - k; ++i) {
        combination.push_back(people[i]);
        go(i + 1, k - 1);
        combination.pop_back();
    }
}



vector<vector<int>> combinationSum3(int k, int n) {
    for (int i = 1; i <= 9; i++) { people.push_back(i); }
    go(0, k);
    vector<vector<int>> comb3;


    // check if == sum
    for (auto i : final_combs) {
        int sum = accumulate(i.begin(), i.end(), 0);
        if (sum == n) {
            comb3.push_back(i);
        }
    }

    return comb3;
}
```


---

# 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/216_combination_sum_iii.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.
