Question: I have come across the following problem statement:

> 
> You have a sentence written entirely in a single row. You would like to split it into several rows by replacing some of the spaces
>  with "new row" indicators. Your goal is to minimize the width of the
>  longest row in the resulting text ("new row" indicators do not count
>  towards the width of a row). You may replace at most K spaces.
> 
> 
> You will be given a sentence and a K. Split the sentence using the
>  procedure described above and return the width of the longest row.
> 
> 
> 

I am a little lost with where to start. To me, it seems I need to try to figure out every possible sentence length that satisfies the criteria of splitting the single sentence up into K lines.

I can see a couple of edge cases:

1. There are <= K words in the sentence, therefore return the longest word.
2. The sentence length is 0, return 0

If neither of those criteria are true, then we have to determine all possible combinations of splitting the sentence and the return the minimum of all those options. This is the part I don't know how to do (and is obviously the heart of the problem).

Answer: 
Easy if you use the Gauss formula to calculate the sum of the first N consecutive numbers:

```

    function solution(A) {
        var np1, perfectSum, sum;

        if (!A || A.length == 0) {
            return 1;
        }

        np1 = A.length + 1;

        // Gauss formula
        perfectSum = np1 * (1 + np1) / 2;

        // Sum the numbers we have
        sum = A.reduce((prev, current) => prev + current);

        return perfectSum - sum;
    }

```
My 100% JavaScript solution:

```
function solution(A) {

    const N = A.length;

    // use the Gauss formula
    const sumNatural = (N + 1) * (N + 2) / 2;

    // actual sum with the missing number (smaller than sumNatural above)
    const actualSum = A.reduce((sum, num) => sum += num, 0);

    // the difference between sums is the missing number
    return sumNatural - actualSum;

}

```