Meaning of the formula how to find lost element in array?

Viewed 254

The task is to find lost element in the array. I understand the logic of the solution but I don't understand how does this formula works?

Here is the solution

int[] array = new int[]{4,1,2,3,5,8,6};
   int size = array.length;
   int result = (size + 1) * (size + 2)/2;
   for (int i : array){
       result -= i;
   }

But why we add 1 to total size and multiply it to total size + 2 /2 ?? In all resources, people just use that formula but nobody explains how that formula works

5 Answers

The sum of the digits 1 thru n is equal to ((n)(n+1))/2.

e.g. for 1,2,3,4,5 5*6/2 = 15.

But this is just a quick way to add up the numbers from 1 to n. Here is what is really going on.

The series computes the sum of 1 to n assuming they all were present. But by subtracting each number from that sum, the remainder is the missing number.

The formula for an arithmetic series of integers from k to n where adjacent elements differ by 1 is.

S[k,n] = (n-k+1)(n+k)/2

Example: k = 5, n = 10

  • S[k,n] = 5 6 7 8 9 10

  • S[k,n] = 10 9 8 7 6 5

  • S[k,n] = (10-5+1)*(10+5)/2

  • 2S[k,n] = 6 * 15 / 2

  • S[k,n] = 90 / 2 = 45

For any single number missing from the sequence, by subtracting the others from the sum of 45, the remainder will be the missing number.

Let's say you currently have n elements in your array. You know that one element is missing, which means that the actual size of your array should be n + 1.

Now, you just need to calculate the sum 1 + 2 + ... + n + (n+1).

A handy formula for computing the sum of all integers from 1 up to k is given by k(k+1)/2.

By just replacing k with n+1, you get the formula (n+1)(n+2)/2.

It's simple mathematics.

Sum of first n natural numbers = n*(n+1)/2.

Number of elements in array = size of array.

So, in this case n = size + 1

So, after finding the sum, we are subtracting all the numbers from array individually and we are left with the missing number.

Broken sequence vs full sequence

But why we add 1 to total size and multiply it to total size + 2 /2 ?

The amount of numbers stored in your array is one less than the maximal number, as the sequence is missing one element.

Check your example:

4, 1, 2, 3, 5, 8, 6

The sequence is supposed to go from 1 to 8, but the amount of elements (size) is 7, not 8. Because the 7 is missing from the sequence.

Another example:

1, 2, 3, 5, 6, 7

This sequence is missing the 4. The full sequence would have a length of 7 but the above array would have a length of 6 only, one less.

You have to account for that and counter it.


Sum formula

Knowing that, the sum of all natural numbers from 1 up to n, so 1 + 2 + 3 + ... + n can also be directly computed by

n * (n + 1) / 2

See the very first paragraph in Wikipedia#Summation.

But n is supposed to be 8 (length of the full sequence) in your example, not 7 (broken sequence). So you have to add 1 to all the n in the formula, receiving

(n + 1) * (n + 2) / 2

I guess this would be similar to Missing Number of LeetCode (268):

Java

class Solution {
    public static int missingNumber(int[] nums) {
        int missing = nums.length;

        for (int index = 0; index < nums.length; index++)
            missing += index - nums[index];

        return missing;
    }
}

C++ using Bit Manipulation

class Solution {
public:
    int missingNumber(vector<int> &nums) {
        int missing = nums.size();
        int index = 0;

        for (int num : nums) {
            missing = missing ^ num ^ index;
            index++;
        }

        return missing;
    }
};

Python I

class Solution:
    def missingNumber(self, nums):
        return (len(nums) * (-~len(nums))) // 2 - sum(nums)

Python II

class Solution:
    def missingNumber(self, nums):
        return (len(nums) * ((-~len(nums))) >> 1) - sum(nums)

Reference to how it works:

The methods have been explained in the following links:

Missing Number Discussion

Missing Number Solution

Related