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 - II Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Subsets - II

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

Problem Statement

Given an array of integers A, return all possible subsets. The array might contain duplicates. 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. After forming the subset, we check in our current list of subsets if this subset is present or not, if yes, then it is discarded, else added into our list. This technique is known as Bitmasking. We can also solve it recursively using a backtracking-based approach.

Analysis

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

Implementation

Recursive Approach 

C++

void getSubsets(vector<int> &A, int start, vector<int> currSubset, vector<vector<int>> &allSubsets) {
	if (start == A.size()) {
		allSubsets.push_back(currSubset);
		return;
	}
	int indx = 1;
	while (start + indx < A.size() && A[start] == A[start + indx]) {
		indx++;
	}
	getSubsets(A, start + indx, currSubset, allSubsets);
	currSubset.push_back(A[start]);
	getSubsets(A, start + 1, currSubset, allSubsets);
}
vector<vector<int>> subsets(vector<int> &A) {
	sort(A.begin(),A.end());
	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;
		}
		int indx = 1;
		while (start + indx < A.length && A[start] == A[start + indx]) {
			indx++;
		}
		getSubsets(A, start + indx, currSubset, allSubsets);
		currSubset.add(A[start]);
		getSubsets(A, start + 1, currSubset, allSubsets);
		currSubset.remove(currSubset.size() - 1);
	}
	List<List<Integer>> subsets(int[] A) {
		Arrays.sort(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]);
            }
        }
		sort(subsets.begin(), subsets.end());
		if(find(allSubsets.begin(), allSubsets.end(), subsets) == allSubsets.end()) {
        	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) {
		Arrays.sort(A);
		int n = A.length;
		List<List<Integer>> list = new ArrayList<>();
		for(int mask = 0; mask < Math.pow(2,n); mask++){
			List<Integer> subsets = new ArrayList<>();
			for(int i = 0; i < n; i++) {
				if(((1 << i) & mask) != 0) {
					subsets.add(A[i]);
				}
			}
			if(checkIfValid(list, subsets)) {
				list.add(subsets);
			}
		}
		return list;
	}
	boolean checkIfValid(List<List<Integer>> list, List<Integer> subsets){
		int flag = 0;
		for(List<Integer> i: list){
			int counter = 0;
			if(i.size() != subsets.size()) {
				continue;
			} else {
				flag = 1;
				for(int j = 0 ; j < subsets.size(); j++) {
					if(i.get(j) == subsets.get(j)) {
						counter++;
					}
				}
				if(counter == subsets.size()) {
					return false;
				}
			}
		}
		return true;
	}
}
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
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