Let's say we have such an array: myArray = [A, A, B, B, C, C, D, E]
I would like to create an algorithm so that it will find all the combinations that add up to the whole array, where none of the elements are repeated.
Example combinations:
[A, B, C, D, E] [A, B, C]
[A, B, C, D] [A, B, C, E]
[A, B, C] [A, B, C] [D, E]
Clarification: [A, B, C] [A, B, C] [D, E]
and [A, B, C] [D, E] [A, B, C]
are the same combinations. Also the ordering with the subsets doesn't matter as well. For example [A,B,C]
and [B,A,C]
should be the same.
So far, I didn't progress beyond
var myArray = ["A", "A", "B", "B", "C", "C", "D", "E"]
console.log([...new Set(myArray)])
But this doesn't help at all, it just returns one distinct set. I couldn't find a similar problem posted before, so could anyone guide me here in how to achieve this?
Some Basic Functions associated with multiset: begin () – Returns an iterator to the first element in the multiset –> O (1) end () – Returns an iterator to the theoretical element that follows the last element in the multiset –> O (1) size () – Returns the number of elements in the multiset –> O (1)
View Disk Partitions with Windows Disk Management Step 1: On the keyboard, press Windows + R. Then type "diskmgmt. msc"... Full steps 3. View, Unhide, or Recover Partitions on A Hard Drive via Software Step 1. Open EaseUS Partition Master and click " Partition Recovery "... Full steps
When you install Windows on a new hard drive, the installer perceives your disk as a large amount of unallocated space. It would help if you made a segment so that the operating system understands which part of the hard drive it can access. This is referred to as a partition.
Let's have a look at the steps on how to check partitions via Windows explorer. Step 1: First navigate to Windows Explorer by Press Windows + E keys. Step 2: Then you can view the partition size of each hard drive as shown in the below image. What you can see: One can see only partitions and partition size.
I'm getting 315 combinations. Is that right? :)
Here's a recursion:
function distribute(e, i, _comb){
// No more e's
if (e[1] == 0)
return [_comb];
// We're beyond the combination
if (i == -1)
return [_comb.concat([e])];
let result = [];
for (let c=1; c<=Math.min(_comb[i][1], e[1]); c++){
let comb = _comb.map(x => x.slice());
if (c == comb[i][1]){
comb[i][0] += e[0];
} else {
comb[i][1] -= c;
comb.push([comb[i][0] + e[0], c]);
}
result = result.concat(distribute([e[0], e[1] - c], i - 1, comb));
}
let comb = _comb.map(x => x.slice());
return result.concat(distribute(e, i - 1, comb));
}
function f(arr){
function g(i){
if (i == 0)
return [[arr[0]]];
const combs = g(i - 1);
let result = [];
for (let comb of combs)
result = result.concat(
distribute(arr[i], comb.length - 1, comb));
return result;
}
return g(arr.length - 1);
}
function show(arr){
const rs = f(arr);
const set = new Set();
for (let r of rs){
const _r = JSON.stringify(r);
if (set.has(_r))
console.log('Duplicate: ' + _r);
set.add(_r);
}
let str = '';
for (let r of set)
str += '\n' + r
str += '\n\n';
console.log(JSON.stringify(arr));
console.log(set.size + ' combinations:');
console.log(str);
}
show([['A', 2], ['B', 2], ['C', 2], ['D', 1], ['E', 1]]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With