VOOZH about

URL: https://www.geeksforgeeks.org/dsa/powet-set-lexicographic-order/

⇱ Power Set in Lexicographic order - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Power Set in Lexicographic order

Last Updated : 23 May, 2024
This article is about generating Power set in lexicographical order. 

Examples :

Input : abc
Output : a ab abc ac b bc c

The idea is to sort array first. After sorting, one by one fix characters and recursively generates all subsets starting from them. After every recursive call, we remove last character so that next permutation can be generated. 

Implementation:


Output
a ab b c ca cab cb 

Time Complexity: O(n*2n
Auxiliary Space: O(1)

Method (binary numbers)

The idea is to use binary numbers  to generate the power set of a given set of elements in lexicographical order

  • Sort the given set in lexicographical order.
  • Define a variable "n" to represent the size of the set.
  • Use a loop to generate all possible binary numbers of length "n".
  • For each binary number, convert it to a string of 0s and 1s, 
  • Add the current subset to the output list.
  • Sort the output list in lexicographical order.
  • Print the sorted list of subsets.

Output
 a ab abc ac b bc c 

Time complexity :O(2^n * n), where n is the length of the input set.
Space complexity  :O(2^n * n), since the output list of subsets can potentially contain 2^n elements

Comment
Article Tags: