diff --git a/chapter-8-recursion-and-Dynamic-Programming/8-4-Power-Set.cpp b/chapter-8-recursion-and-Dynamic-Programming/8-4-Power-Set.cpp new file mode 100644 index 0000000..33df65e --- /dev/null +++ b/chapter-8-recursion-and-Dynamic-Programming/8-4-Power-Set.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +std::set> subsets; + +void print(const std::set& set) { + for (const auto& i : set) { + std::cout << i << " "; + } +} + +void findSubsets(const std::set& set) { + subsets.emplace(set); + + if (set.size() <= 1) + return; + + for (unsigned int i = 0; i < set.size(); ++i) { + std::set newSubset; + for (unsigned int j = 0; j < set.size() - 1; ++j) { + std::set::iterator it = set.begin(); + std::advance(it, (j + i) % set.size()); + newSubset.emplace(*it); + } + + findSubsets(newSubset); + + } + +} + +int main(int argc, char** argv) { + std::set set{ 0, 1, 2 }; + findSubsets(set); + + for (const auto& set : subsets) { + print(set); + std::cout << std::endl; + } + + std::cin.get(); + + return 0; +} \ No newline at end of file