fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. // Define the original array - you can change the elements as needed
  7. vector<int> arr = {1, 2, 3};
  8. int n = arr.size();
  9.  
  10. // Total number of subsets = 2^n
  11. int totalSubsets = 1 << n;
  12.  
  13. cout << "All subsets:" << endl;
  14.  
  15. // Loop through all possible bitmasks from 0 to 2^n - 1
  16. for (int mask = 0; mask < totalSubsets; ++mask) {
  17. cout << "{ ";
  18.  
  19. // Check each bit of the mask
  20. for (int i = 0; i < n; ++i) {
  21. // If the i-th bit is set in the current mask,
  22. // include arr[i] in the current subset
  23. if (mask & (1 << i)) {
  24. cout << arr[i] << " ";
  25. }
  26. }
  27.  
  28. cout << "}" << endl;
  29. }
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
All subsets:
{ }
{ 1 }
{ 2 }
{ 1 2 }
{ 3 }
{ 1 3 }
{ 2 3 }
{ 1 2 3 }