#recursing a subset of an array to obtain the next subset; return and take the next numbers subset.

4 messages · Page 1 of 1 (latest)

onyx orbit
#

Hello, I'm working on a project, and have come to a halt.
I need to get an array of length k, and I need to take that arrays subset if the i'th number was not there, (note; not popping the number, storing it in a string or list): but im lost on how to make a subset AND how to recurse this so after it finishes one branch that it goes up and back down the next branch.

this is my code so far:

public void rec(int k, int index[]){

        if (k == 1){
            return;
        }
        int[] subset = new int[index.length-1]; //creation of subset
        for (int p = index.length-1; p >= 0; p--) {
            subset[p] = index[p];
        }

        for (int i = 0; i < subset.length; i++) {
            //Loop throgh the subsets va; so that we may anchor the next var

            rec(k-1, subset[]);//take the last 2 from the index
        }

    }

To re iterate, I need to;
take an array, get the first val of that array, store it, then create a subset of the array, recurse the subset, after weve gotten to the last value of the array, go back up and go down the next tree. with the NEXT value of the previous subset.

royal radishBOT
#

This post has been reserved for your question.

Hey @onyx orbit! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

onyx orbit
#

I've concluded that this may be an answer (for now)

public void rec(int k, int index[]){

        if (k == 1){
            return;
        }


        int anchor = 0;
        int[] subset = new int[index.length-1]; //creation of subset
        for (int w = 0; w < index.length; w++) {
            anchor = index[w];
            for (int p = index.length-1; p >= 0; p--) {
                subset[p] = index[p];
            }
            rec(k-1, subset);
        }
        System.out.println(anchor);
    }

If you notice a discrepancy, please let me know ASAP