The Gauss Sum, and Solving for the Missing Number
Tags: algorithms, leetcode, javascript, codenewbie
Published: 2020-06-24 18:17:58

Today's algorithm is the [Missing Number problem](https://leetcode.com/problems/missing-number/):
> Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

So, if you were given the array `[4, 1, 0, 2, 5]`, the function should return 3, since that's the missing number in the consecutive order.

Typically when solving algorithms, I try to approach them using methods that are very applicable to a wide range of problems. Every so often, however, I really like a solution that utilizes an established formula or algorithm, particularly if I feel that formula can be used in a number of different ways. To solve this problem, I'll be using something called the "Gauss Sum", a trick that comes in handy when solving a range of number-based algorithms.

## Gauss's Sum, and How to Approach This Problem
The story behind the Gauss Sum is that there once was a child named Carl Gauss, and when he was in grade school he was asked to sum all of the numbers from 1 to 100. He quickly responded that the answer was 5050, after picking up on a pattern: summing the first and last number in the series was 101. Summing the second and second to last number in the series was 101, and so on. (You can read more about it [here](https://www.nctm.org/Publications/Teaching-Children-Mathematics/Blog/The-Story-of-Gauss/).)

![Numbers 1, 2, 3 on the left side in black, and 98, 99, 100 on the right side in black, with an ellipses (...) in between them. A blue arch connects 1 and 100, and above it is "1 + 100 = 101" in blue. A red arch connects 2 and 99, and above it is "2 + 99 = 101" in red. A green arch connects 3 and 98, and above it is "3 + 98 = 101" in green.](https://dev-to-uploads.s3.amazonaws.com/i/qw12k83u88elo3cck7b7.png)

In other words, if you want to find the sum of all of the consecutive numbers from 0 to `n`, you can use the formula:
`sum = (n * (n + 1)) / 2`

In this problem, we can find the "missing number" by finding the Gaussian Sum of the numbers, finding the actual sum of the numbers, and returning the difference.

For example, if the given array `nums` was `[2, 0, 3]`, the Gaussian Sum would be `(3 * (3 + 1)) / 2`, which is 6. (Why did we know that n = 3? Since only one number is missing from the array, and the array starts counting at 0, we know that the largest number, n, in the array is equal to the length of the array.) The actual sum of the digits in the array is 5 (2 + 0 + 3). The difference between the Gaussian sum and the actual sum is 1, which is our missing number.

## Coding the Solution

The code for this solution is actually just three lines -- but, of course, that doesn't mean it's simple. The first thing we'll do is calculate the Gaussian Sum.

```javascript
function missingNumber(nums) {
  const gaussSum = (nums.length * (nums.length + 1)) / 2;
  //...
}
```

Now, we want to calculate the actual sum of the digits in the `nums` array. To do that, we can use `.reduce()`, a method which can find the sum total of elements in an array. There's a lot you can do with `.reduce()`, and you can learn more about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce), but for the purposes of this problem, we're going give it two arguments: an accumulator and a current value.

The accumulator keeps track of the sum total of values that have been seen, and is ultimately returned by the function. The current value is the current element we're on in the array. `.reduce()` uses the callback function that's passed into it to perform an execution on each current element. So, in this problem, we want to sum all of the elements in the array, which means that the callback function will be `accumulator + currentValue`.

```javascript
function missingNumber(nums) {
  const gaussSum = (nums.length * (nums.length + 1)) / 2;
  const actualSum = nums.reduce((accumulator, currentValue) => accumulator + currentValue);
  //...
}
```

Finally, we can return the difference between `gaussSum` and `actualSum`, which is the missing number in the array.

```javascript
function missingNumber(nums) {
  const gaussSum = (nums.length * (nums.length + 1)) / 2;
  const actualSum = nums.reduce((accumulator, currentValue) => accumulator + currentValue);
  return gaussSum - actualSum;
}
```

--
There's definitely a number of different ways this algorithm can be solved, so please let me know if you have any questions or other solutions to this problem!