Creating all possible k combinations of n items in C++

From Rosetta code #include <algorithm> #include <iostream> #include <string> void comb(int N, int K) { std::string bitmask(K, 1); // K leading 1’s bitmask.resize(N, 0); // N-K trailing 0’s // print integers and permute bitmask do { for (int i = 0; i < N; ++i) // [0..N-1] integers { if (bitmask[i]) std::cout << ” ” … Read more

Javascript – Generating all combinations of elements in a single array (in pairs)

Here are some functional programming solutions: Using EcmaScript2019’s flatMap: var array = [“apple”, “banana”, “lemon”, “mango”]; var result = array.flatMap( (v, i) => array.slice(i+1).map( w => v + ‘ ‘ + w ) ); console.log(result); Before the introduction of flatMap (my answer in 2017), you would go for reduce or [].concat(…) in order to flatten … Read more

Generate all binary strings of length n with k bits set

This method will generate all integers with exactly N ‘1’ bits. From https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Compute the lexicographically next bit permutation Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the … Read more

Generate all combinations from multiple lists

You need recursion: Let’s say all your lists are in lists, which is a list of lists. Let result be the list of your required permutations. You could implement it like this: void generatePermutations(List<List<Character>> lists, List<String> result, int depth, String current) { if (depth == lists.size()) { result.add(current); return; } for (int i = 0; … Read more

Generating combinations in C++

A simple way using std::next_permutation: #include <iostream> #include <algorithm> #include <vector> int main() { int n, r; std::cin >> n; std::cin >> r; std::vector<bool> v(n); std::fill(v.end() – r, v.end(), true); do { for (int i = 0; i < n; ++i) { if (v[i]) { std::cout << (i + 1) << ” “; } } … Read more

JavaScript – Generating combinations from n arrays with m elements [duplicate]

Here is a quite simple and short one using a recursive helper function: function cartesian(…args) { var r = [], max = args.length-1; function helper(arr, i) { for (var j=0, l=args[i].length; j<l; j++) { var a = arr.slice(0); // clone arr a.push(args[i][j]); if (i==max) r.push(a); else helper(a, i+1); } } helper([], 0); return r; } … Read more

Faster alternative to nested loops?

As a reminder: you probably do not need this kind of code while developing your own solution. This can and should only used in very specific situations. Readability is often more important than speed. You can use the properties of a struct and allocate the structure in advance. I cut off some levels in the … Read more

Combination of two arrays in Ruby

You can use product to get the cartesian product of the arrays first, then collect the function results. a.product(b) => [[1, 3], [1, 4], [2, 3], [2, 4]] So you can use map or collect to get the results. They are different names for the same method. a.product(b).collect { |x, y| f(x, y) }