Print the power set of a set of distinct integers.

Print the power set of a set of distinct integers. The power set of a set is the set of all its subsets.

Problem

Given a set of distinct integers, print the power set of the set. The power set of a set is the set of all its subsets.
Example input:
[1, 2, 3]
Example output:
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Solution

This solution is in C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <vector>

using namespace std;

void print_power_set(vector<int>& set) {
    int n = set.size();
    int num_subsets = 1 << n;
    for (int i = 0; i < num_subsets; i++) {
        cout << "[";
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                cout << set[j] << " ";
            }
        }
        cout << "]" << endl;
    }
}

int main() {
    vector<int> set = {1, 2, 3};
    print_power_set(set);
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The approach is to use bitwise operators to generate all possible subsets. This is a clever approach that demonstrates a good understanding of bitwise operators.

Evaluated at: 2022-11-27 12:15:46