1
var combinations = function(numArr, choose, callback) {
    var n = numArr.length;
    var c = [];
    var inner = function(start, choose_) {
        if (choose_ == 0) {
            callback(c);
        } else {
            for (var i = start; i <= n - choose_; ++i) {
                c.push(numArr[i]);
                inner(i + 1, choose_ - 1);
                c.pop();
            }
        }
    }
    inner(0, choose);
}

I'm not entirely sure how I would append all the items inside an array after it's done creating all the combinations.

I attempted some modifications to the code though I ultimately ended up messed it up.

Example :

wateraura
  • 331
  • 1
  • 2
  • 8

2 Answers2

2

So, are you trying to do something like this: http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/

I found a double-loop implementation accomplishes the task pretty well:

function findCombinations(nums, cb) {
    var allCombinations = [];
    var maxIndex = nums.length - 1;
    var i = 0, j = 0;
    for (i; i <= maxIndex; i += 1) {
        for (j = i; j <= maxIndex; j += 1) {
            if (i !== j) {
                allCombinations.push([ nums[i], nums[j] ]);
            }
        }
    }
    cb(allCombinations);
}

findCombinations([1, 2, 3, 4], function (combinations) {
    console.log(combinations);
});

Prints the output:

[ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ] ]

Were you specifically trying to implement a recursion tree?

Nicholas Cloud
  • 1,564
  • 1
  • 13
  • 19
  • Not exactly, I need it to be able to change the number of combinations like instead of 2 I need it to be out of 3 combinations. I guess I can follow your example and add recursion I suppose. I want to able to modify the number of elements within each combination* – wateraura Oct 06 '14 at 03:12
1

If this is the right output, I got it to work with a minor fix to your solution. Your approach is totally correct. You just need to think about when you're beginning/ending a combination. When a combination ends you need to push the current combination to an external array. Your base case signifies when a combination is complete, at this point you need to push a COPY of the current combination.

> combinations([0,1,2,3],2)
[ [ 0, 1 ],
  [ 0, 2 ],
  [ 0, 3 ],
  [ 1, 2 ],
  [ 1, 3 ],
  [ 2, 3 ] ]
> combinations([0,1,2,3],3) 
[ [ 0, 1, 2 ],
  [ 0, 1, 3 ],
  [ 0, 2, 3 ],
  [ 1, 2, 3 ] ]

Here is the tweaked solution. You could use the callback on c if you'd like.

function combinations(numArr, choose, callback) {
    var n = numArr.length;
    var c = [];
    var temp = [];
    var inner = function(start, choose_) {
        if (choose_ === 0) {
            c.push(temp.slice());
        } else {
            for (var i = start; i <= n - choose_; i++) {
                temp.push(numArr[i]);
                inner(i + 1, choose_ - 1);
                temp.pop();
            }
        }
    }
    inner(0, choose);
    return c;
}
cdosborn
  • 3,111
  • 29
  • 30
  • It is the right output though I kind of followed an algorithm that I saw online though I didn't exactly know how to output all of them together when ending so... – wateraura Oct 06 '14 at 03:24