• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Print Power Set of a Set

April 18, 2015 by Dhaval Dave

Print Power Set of a Set of character.

Power Set Power set P(S) of a set S is the set of all subsets of S. For example S = {a, b, c} then P(s) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}.

If S has n elements in it then P(s) will have 2^n elements

 

Method 1:

Input: Set[], set_size
1. Get the size of power set
    powet_set_size = pow(2, set_size)
2  Loop for counter from 0 to pow_set_size
     (a) Loop for i = 0 to set_size
          (i) If ith bit in counter is set
               Print ith element from set for this subset
     (b) Print seperator for subsets i.e., newline

#include <stdio.h>
#include <math.h>
 

voidprintPowerSet(char*set, intset_size)
{
    /*set_size of power set of a set with set_size
      n is (2**n -1)*/
    unsigned intpow_set_size = pow(2, set_size);
    intcounter, j;
    /*Run from counter 000..0 to 111..1*/
    for(counter = 0; counter < pow_set_size; counter++)
    {
      for(j = 0; j < set_size; j++)
       {
          /* Check if jth bit in the counter is set
             If set then pront jth element from set */
          if(counter & (1<<j))
            printf("%c", set[j]);
       }
       printf("n");
    }
}
/*Driver program to test printPowerSet*/
intmain()
{
    charset[] = {'a','b','c'};
    printPowerSet(set, 3);
    getchar();
    return0;
}

Method 2:  We can use Recursion and with understanding of Binary Tree , we can format it.
Example : 

                   {a,b,c} print set= abc
                    / \ 
                   /   \ 
                  /     \ 
               {a,b}    {b,c}  print set = ab,bc
                /\        /\ 
               /  \      /  \ 
              {a}  {b} {b}  {c} print set
          
void printPowerSet(char *set, int l , int h , int size)
{
    int i=l;
    for(i=l;i<=h;i++){printf("%c",set[i]);}
     printf("n");
     if(l+1 < size){ 
          printPowerSet(set,l+1,h,size); 
     } 
     if(h-1 >= 0 ){
            printPowerSet(set,l,h-1,size);
     }
}//printpowerset

int main(void) {
char set[] = {'a','b','c','d'};
printPowerSet(set,0,3,4 );
 
getchar();
 
return 0;
}

I have run it here…
http://ideone.com/e.js/faEPpM

 

 

 

 

Similar Articles

Filed Under: Adobe Interview Questions, Hacker Earth Questions, Interview Questions, problem Tagged With: Array, Recursion, string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Amazon Interview On-Campus For Internship – 1

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Facebook Interview Question : Interleave List

Check Binary Tree is Binary Search Tree or not

SAP Off Campus Hiring_ March 2015 Verbal Skills

Amazon Interview Experience – SDE Chennai

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

DFS (Depth First Search)

Python List

Python Array String

Best Java Book | Top Java Programming Book for Beginners

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Python String and numbers

Find min element in Sorted Rotated Array (With Duplicates)

TicTacToe Game As Asked in Flipkart

K’th Largest Element in BST when modification to BST is not allowed

Practo Hiring Experience

Get K Max and Delete K Max in stream of incoming integers

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Stock Buy Sell to Maximize Profit

Sort Stack in place

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Find position of the only set bit

Wrong Directions given find minimum moves so that he can reach to the destination

Find min element in Sorted Rotated Array (Without Duplicates)

System Design: Designing a LLD for Hotel Booking

LeetCode: Container With Most Water

Cisco Hiring Event 21st – 22nd Feb 2015

Microsoft BING Interview Experience

Memory Efficient LinkedList

Copyright © 2023 · Genesis Framework · WordPress · Log in