All Possible Subsets of a Set of Distinct Integers

This problem asks us to find all possible subsets of a given set of distinct integers. For example, if the input is [1,2,3], then the output should be [[ ], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]].

Problem

Given a set of distinct integers, return all possible 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;

vector<vector<int>> subsets(vector<int>& nums) {
    vector<vector<int>> res;
    vector<int> cur;
    res.push_back(cur);
    for (int i = 0; i < nums.size(); i++) {
        int n = res.size();
        for (int j = 0; j < n; j++) {
            cur = res[j];
            cur.push_back(nums[i]);
            res.push_back(cur);
        }
    }
    return res;
}

int main() {
    vector<int> nums = {1, 2, 3};
    vector<vector<int>> res = subsets(nums);
    for (int i = 0; i < res.size(); i++) {
        for (int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] << " ";
        }
        cout << end l;
    }
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The approach is to iterate through the input array and create new subsets by adding each element to all existing subsets.

Evaluated at: 2022-12-08 02:15:42