Not sure if this is the best algo ..

Create n lists - i'th list will have all subsets of length i.

Sort your elements in ascending order such that first element is smallest.

Start a counter from 0 to 2^n-1 and assign each bit in the number to a
particular element. The least significant bit should map to first
(smallest) element and most significant bit should map to last
(largest) element. Now, for each number in the counter, construct a
subset by seeing which all bits are 1 and adding those particular
elements in the subset. Add the subset to the corresponding list,
based on its size.

Print the list in order.

On 9 May 2011 20:26, Amir Hossein Sharifzadeh <[email protected]> wrote:
> Dear Friends,
>
> I need to a program to find all sorted subsets of a set. There are some
> recursion solutions for generating subsets, but those are not sorted.
>
> However I attend a solution to show me all of sorted subsets of a set.
> Assume, set includes of numeric values.
>
> For example:
>
> input numbers = {1,2,3,4}
>
> output would be shown as follow down:
>
> 1
> 2
> 3
> 4
>
> 1,2
> 1,3
> 1,4
> 2,3
> 2,4
> 3,4
>
> 1,2,3
> 1,2,4
> 1,3,4
> 2,3,4
>
> 1,2,3,4
>
> I will be very pleased, if someone could provide me with a solution (in
> java, c or c++)
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to