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: 
Here is 100% javascript solution:

```
function solution(A) {
    if (!A.length) return 1;
    let n = A.length + 1;
    return (n + (n * n - n) / 2) - A.reduce((a, b) => a + b);
}

```

We return 1 if the given array is empty, which is the missing element in an empty array.

Next we calculate the 'ordinary' series sum, assuming the first element in the series is always 1. Then we find the difference between the given array and the full series, and return it. This is the missing element.

The mathematical function I used here are series sum, and last element:

* Sn = (n/2)\*(a1 + an)
* an = a1 + (n - 1)\*d
Get 100 in correctness and performance using this function

```
function solution(A) {
    // write your code in JavaScript (Node.js 4.0.0)
    var size = A.length;
    var sum = (size + 1) * (size + 2) / 2;
    for (i = 0; i < size; i++) {
        sum -= A[i];
    }
    return sum;
}

```