0

I am given an array with 49 My task is to select unique groups of 7 elements and also find out the number of possible outputs.

Eg. [A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y]

Then outputs: [ [A,a,B,b,C,c,D], [a,B,b,C,c,D,d], [B,b,C,c,D,d,E], [b,C,c,D,d,E,e], . . . [A,C,c,D,d,E,e], [A,B,b,c,D,d,E], . . . ] How do I get this outputs? Below is what I have tried based on the answer i got from stackoverflow:

let jar = ["A","a","B","b","C","c","D","d","E","e","F","f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y"];
 
const size = 7;

let result11 = [];

for(let i = 0; i <= (jar.length - size); i++){
     result11.push(jar.slice(i, size+i));
}
console.log(result11)

Each of the output should be unique such that there should not be any repetition. Eg. outputs like aaaaaaa, aaxxxYY, AAAAAAA, AABbcDe are not valid but outputs like avWwXxY,bvWwXxY,cvWwXxY alongside others etc

NnaDid
  • 15
  • 1
  • 6
  • The output you show as desired and the one you get from the code seem to be equal. So what is wrong with your code? – t.niese Jun 29 '22 at 15:28
  • @t.niese the output does not include all possiblities for eg. [A,v,W,w,X,x,Y], and the likes are not in the output – NnaDid Jun 29 '22 at 15:32
  • Does this answer your question? [Split array into chunks](https://stackoverflow.com/questions/8495687/split-array-into-chunks) – Tom Kay Jun 29 '22 at 15:34
  • 1
    The number of possible results is given by the binomial coefficient or "49 over 7", see here: https://en.m.wikipedia.org/wiki/Binomial_coefficient . In this case the formula goes: `49*48*47*46*45*44*43/(7*6*5*4*3*2)=85900584` – Carsten Massmann Jun 29 '22 at 15:35
  • It is still not clear what combinations are valid and which one not. What about `aaaaaaa`, or `ZzyYXxwW`? – t.niese Jun 29 '22 at 15:44
  • please use a smaller set of data and a smaller size and add a comlete wanted result ser from it. – Nina Scholz Jun 29 '22 at 16:04
  • @CarstenMassmann *aaaaaaa* is not valid since repetition is not allowed, if you look at the example of the output Gave above, each ouput should not have an element aprearing more than once – NnaDid Jun 29 '22 at 16:34
  • is `'aba....'` valid? – Nina Scholz Jun 29 '22 at 17:05
  • I am completely aware that `aaaaaaa` would not be allowed. The question came from @t.niese and not from me. My count of 85900584 possible solutions is based on non-repeating combinations, ignoring their internal order. – Carsten Massmann Jun 29 '22 at 20:52

1 Answers1

1

Below you will find a working snippet that will generate all unique draws from a given array. Starting point is the initial draw vector v. This vector must contain exactly the number of array elements you want to have in each draw and the numbers must be in ascending order. Based on this initial vector the function nextDraw() will pick the next possible combination from the available number pool. The number pool is defined by 0 as the lower limit and n-1 as the upper limit.

My snippet is currently limited to 1000 draws as most of you would probably loose interest if you had to wait for all 85900584 possible combinations to be calculated and printed here.

function nextDraw(v,n){
 // generate a new vector w by copying v
 // set k to point to the last element of w:
 let w=v.slice(0),l=w.length,k=l-1;
 while (true){ // unconditional loop
  // Is the k-th element of w lower than n-l+k?
  if (w[k]<n-l+k) {
   // increment w[k]
   w[k]++;
   // and, in case k<l-1:
   // initialise all following elements of w with an increasing sequence:
   for (let j=k+1;j<l;j++) {
    w[j]=w[j-1]+1;
   }
   // return a valid draw vector!
   return w;
  }
  else {
   // as long as there is still a smaller k index available: decrement k
   // and continue with the while loop
   if(k) k--
   // else: we have reached the end of the series! 
   else return false;
  }
 }
}
function allDraws(v,n){
 const res=[];
 res.push(v);
 while (res.length<1000 && (v=nextDraw(v,n))) res.push(v)
 return res;
}

// Show first 1000 and last 121 draws:
[[0,1,2,3,4,5,6],[38,43,44,45,46,47,48]].forEach(v=>{
  let res=allDraws(v,49);
  console.log(res.length,res.map(r=>r.join(",")));
});

Admittedly, my snippet works with index numbers and not with an array of arbitrary elements, but you can easily apply the calculated index vectors to retrieve the actual values from your source array of length n.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43