1

Given a set of distinct integers, I want to find all the possible subsets (for [1,2,3], the code should print [1], [1,2], [1,3], [1,2,3], [2], [2,3], [3] (not necessarily in that order).

There are a few solutions (like this one) out there but what I want to do is to re-implement the bellow solution with a new recursion and no for loop by passing around the indexes: (start = 0)

public void forSolution(List<List<Integer>> res, int[] nums, List<Integer> list, int start) {

    for (int i = start; i < nums.length; i++) {
        List<Integer> tmp = new ArrayList<>(list);
        tmp.add(nums[i]);
        res.add(new ArrayList<>(tmp));
        forSolution(res, nums, tmp, i + 1);
    }
}

I thought I need to pass two integers to the method, one for keeping the record of index and the other one for keeping the start point, but I am having problem on when I need to do the index increment (vs start increment). Any help would be appreciated.

Yar
  • 7,020
  • 11
  • 49
  • 69
  • 1
    Possible duplicate of [Generating power set recursively without any loops](https://stackoverflow.com/questions/15498281/generating-power-set-recursively-without-any-loops) – Prune Nov 12 '18 at 23:16
  • @Prune It's not really a duplicate. Please read the whole post before flagging it. – Yar Nov 12 '18 at 23:25
  • @Yar It's an exact duplicate (except your input is a list of integers, not a string of characters). The algorithms are exactly the same. – melpomene Nov 12 '18 at 23:28
  • @Prune I edited the question to emphasis on the difference. Basically I just want to re-implement the `for` solution by passing around the indexes. – Yar Nov 13 '18 at 00:01
  • Check method 2 in this link: https://www.geeksforgeeks.org/recursive-program-to-generate-power-set/ – Ari Nov 13 '18 at 00:08
  • @Ari thanks for the link. That solution is very similar to @melpomene solution but I am looking for the `for` reimplementation. – Yar Nov 13 '18 at 01:30
  • Any loop can be replaced with a recursive call, the conversion is pretty much automatic. – n. m. could be an AI Nov 13 '18 at 07:04
  • @n.m well I know it should be doable I just don't know how :) – Yar Nov 13 '18 at 19:59

1 Answers1

0

I think the algorithm gets easier if you don't bother with indices.

The basic idea is that for any given sublist, each element of the original list is either included or not included. The list of all possible sublists is simply all possible combinations of including / not including each element.

For a recursive implementation, we can consider two cases:

  1. The input list is empty. The empty list only has a single sublist, which is the empty list itself.
  2. The input list consists of a first element x and a list of remaining elements rest. Here we can call our function recursively to get a list of all sublists of rest. To implement the idea of both including and not including x in our results, we return a list consisting of

    • each element of sublists(rest) with x added at the front, representing all sublists of our original list that contain x, and
    • each element of sublists(rest) as is (without x), representing all sublists of our original list that don't contain x.

For example, if the list is [1, 2, 3], we have x = 1 and rest = [2, 3]. The recursive call sublists(rest) produces [2, 3], [2], [3], []. For each of those sublists we

  • prepend x (which is 1), giving [1, 2, 3], [1, 2], [1, 3], [1], and
  • don't prepend x, giving [2, 3], [2], [3], [].

Concatenating those parts gives our total result as [1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], [].

Sample implementation:

use strict;
use warnings;

sub sublists {
    if (!@_) {
        return [];
    }
    my $x = shift @_;
    my @r = sublists(@_);
    return (map [$x, @$_], @r), @r;
}

for my $sublist (sublists 1, 2, 3) {
    print "[" . join(", ", @$sublist) . "]\n";
}

Output:

[1, 2, 3]
[1, 2]
[1, 3]
[1]
[2, 3]
[2]
[3]
[]
melpomene
  • 84,125
  • 8
  • 85
  • 148