> 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/coding_practice_questions/recursion_and_dynamic_programming/power-set.md).

# Power Set

**8.4 Power Set:** Write a method to return all subsets of a set.

* The power set intuitively represents all the possible subcombinations you can have in given set. In other words, the power set represents all possible combinations for k = 0...n, where n is the length of the set.
* The power set gross at pow(2, k), where k is the length of the set.

ex.

```
case 1: {}                            = P(0) (empty set);       2^0 = 1
case 2: {}, {a}                    = P('a');                2^1 = 2
case n: {}, {a}, {b}, {ab}         = P('ab');               2^2 = 4
```

```
' ' a 
' ' a b ab 
' ' a b ab c ac bc abc 
' ' a b ab c ac bc abc d ad bd abd cd acd bcd abcd
```

* Pascals triangle provides us the amount of combinations per level, for any 'choose'. The growth is exponential. &#x20;

![](/files/-LoJInDdpWEFTLbKV-ik)

* Every additional case feeds on the previous cases by appending a additional unique element to the end of the string.

```cpp
vector<string> power_set(string str) {
    int size = pow(2, str.length());
    vector<string> build;//(size);

    build.push_back(" ");

    if (str.length() >= 1) {
        string temp(1, str[0]);
        build.push_back(temp);
    }

    for (int i = 1; i < str.length(); i++) {
        int cur_size = build.size();
        for (int j = 0; j < cur_size; j++) {
            string temp = build[j];
            temp.insert(temp.end(), str[i]);
            //print(build);
            build.push_back(temp);
        }
    }

    return build;
}

int main()
{
    vector<string> p_set = power_set("a");
    print(p_set);

    p_set = power_set("ab");
    print(p_set);

    p_set = power_set("abc");
    print(p_set);

    p_set = power_set("abcd");
    print(p_set);

    p_set = power_set("abcde");
    print(p_set);
}
```


---

# 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:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/recursion_and_dynamic_programming/power-set.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
