Finding N partitions of number M with min and max partitions sizes
The way you have done your algorithm, you could just apply the same operation to your sum variable, rather than reducing the entire array.

  while (sum &gt; number) {
    for (i = 0; i &lt; result.length; i++) {
      if (result[i] &gt; min) {
        result[i]--;
        sum--
        if (sum === number) {
          break;
        }
      }
    }
    sum = result.reduce(add, 0);
  }


Additionally, the last sum = result.reduce is not needed here, as any change in the result array is subsequently reflected in the sum variable a row lower.

  while (sum &gt; number) {
    for (i = 0; i &lt; result.length; i++) {
      if (result[i] &gt; min) {
        result[i]--;
        sum--
        if (sum === number) {
          break;
        }
      }
    }
  }


Lastly, keep in mind if you were to feed your code an impossible situation ("stuff 5 people into 1 house where each house can only contain 3 people at most"), you will get an infinite loop. It's up to you to decide how to handle this.