Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Subsets Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Subsets

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array of distinct integers A, return all possible subsets. Note: The list should not contain any duplicate subsets.

Approach

The Approach to solve this problem is basically brute force to generate all the subsets. We can observe that for every element, we have a choice to either take it in the current subset or not. We can form a mask of length n, where if the ith element of the mask is set, we take that element into our current subset. This technique is known as Bitmasking. We can also use brute force backtracking to solve the problem recursively.

Analysis

  • Time Complexity: O(n * 2n)
  • Space Complexity: O(n * 2n)

Implementation

Recursive Version

C++
void getSubsets(vector<int> &A, int start, vector<int> &currSubset, vector<vector<int>> &allSubsets) {
	if (start == A.size()) {
		allSubsets.push_back(currSubset);
		return;
	}
	getSubsets(A, start + 1, currSubset, allSubsets);
	currSubset.push_back(A[start]);
	getSubsets(A, start + 1, currSubset, allSubsets);
	currSubset.pop_back();
}
vector<vector<int>> subsets(vector<int> &A) {
	vector<vector<int>> allSubsets;
	vector<int> currSubset;
    getSubsets(A, 0, currSubset, allSubsets);
	return allSubsets;
}
Java
class Solution {
	void getSubsets(int[] A, int start, List<Integer> currSubset, List<List<Integer>> allSubsets) {
		if (start == A.length) {
			allSubsets.add(new ArrayList(currSubset));
			return;
		}
		getSubsets(A, start + 1, currSubset, allSubsets);
		currSubset.add(A[start]);
		getSubsets(A, start + 1, currSubset, allSubsets);
		currSubset.remove(currSubset.size() - 1);
	}
	List<List<Integer>> subsets(int[] A) {
		List<List<Integer>> allSubsets = new ArrayList<>();
		List<Integer> currSubset = new ArrayList<>();
		getSubsets(A, 0, currSubset, allSubsets);
		return allSubsets;
	}
}

Iterative Implementation

C++
vector<vector<int>> bitmask(vector<int> &a, int n) {
    vector<vector<int>> allSubsets;
    for(int mask = 0; mask < (1LL << n); mask++) {
		vector<int> subsets;
        for(int i = 0; i < n; i++) {
            if((1LL << i) & mask) {
                subsets.push_back(a[i]);
            }
        }
        allSubsets.push_back(subsets);
    }
    return allSubsets;
}
vector<vector<int> > subsets(vector<int> &A) {
    vector<vector<int>> result = bitmask(A, A.size());
	return result;
}
Java
class Solution {
	List<List<Integer>> subsets(int[] A) {
		List<List<Integer>> answer = new ArrayList<>();
		int n = A.length;
		for(int mask = 0; mask <= Math.pow(2,n) - 1; mask++) {
			List<Integer> subsets = new ArrayList<>();
			for(int i = 0; i < n; i++) {
				if((1<<i & mask) != 0) {
					subsets.add(A[i]);
				}
			}
			answer.add(subsets);
		}
		return answer;
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Generate Parentheses
Kth Permutation Sequence
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet