Quick and dirty Java code:

import java.util.Arrays;

public class Subset {
        public static void main(String[] args) {
                int[] set = { 1, 2, 3, 4 };
                boolean[] mask = new boolean[set.length];
                int[] picks = new int[set.length];
                subset(picks, 0, set, mask);
        }

        static void subset(int[] picks, int n, int[] set, boolean[] mask) {
                System.out.println(Arrays.toString(Arrays.copyOfRange(picks, 0, 
n)));
                if (n != set.length) {
                        for (int i = 0; i < mask.length; i++) {
                                if (!mask[i]) {
                                        mask[i] = true;
                                        picks[n] = set[i];
                                        subset(picks, n + 1, set, mask);
                                        mask[i] = false;
                                }
                        }
                }
        }
}


--
Best Regards,
Monang




On Mon, May 9, 2011 at 10:56 AM, 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