
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find all distinct subsets of a given set in C++
Here we will see how to display all distinct subsets of a given set. So if the set is {1, 2, 3}, then the subsets will be {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}. The set of all subsets is called power set. The power set has 2n elements.
We will loop through 0 to 2n (excluding), in each iteration we will check whether the ith bit in the current counter is set, then print ith element.
Example
#include<iostream> #include<cmath> using namespace std; void showPowerSet(char *set, int set_length) { unsigned int size = pow(2, set_length); for(int counter = 0; counter < size; counter++) { cout << "{"; for(int j = 0; j < size; j++) { if(counter & (1<<j)) cout << set[j] << " "; } cout << "}" << endl; } } int main() { char set[] = {'a','b','c'}; showPowerSet(set, 3); }
Output
{} {a } {b } {a b } {c } {a c } {b c } {a b c }
- Related Articles
- Print all subsets of given size of a set in C++
- How to find all subsets of a set in JavaScript?
- Python program to get all subsets of given size of a set
- Python program to get all subsets of a given size of a set
- Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers
- C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
- List all the subsets of a set {m , n}
- How to find the distinct subsets from a given array by backtracking using C#?
- Find all distinct palindromic sub-strings of a given String in Python
- Golang program to find all subsets of a string
- Sum of all subsets of a set formed by first n natural numbers
- Java Program To Find all the Subsets of a String
- Count number of subsets of a set with GCD equal to a given number in C++
- Print All Distinct Elements of a given integer array in C++
- Count subsets having distinct even numbers in C++

Advertisements