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

Boris Ruzanov
:

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

WJS
:

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to find an element in an array in mongodb?

From Dev

How to find the largest element of an array of known size?

From Java

How to find index of all occurrences of element in array?

From Dev

How to use XPath expression to find element in an array

From Dev

How to find number of element in an Array in java?

From Dev

how to find a particular string in an element of array in python

From Dev

How to find index of an Array element in OCaml

From Dev

How to find the last element in an under capacity array?

From Dev

How to find first instance of element array

From Dev

How to find the minimum and maximum element of the array?

From Dev

how to find an element in array of object with streams

From Dev

how to find already values in the array element

From Dev

How to use XPath expression to find element in an array

From Dev

How to find element in string(char array) and double it?

From Dev

how to find a[i]th element in array b

From Dev

How to I find a certain element in an array and change it?

From Dev

how to find middle element of array in javascript?

From Dev

ArangoDB - how find element in collection by substring of element of array?

From Dev

How to find the nearest larger element to another element in an array?

From Dev

how to find id of array element from element text/value

From Dev

How to find the next element given a certain element in array

From Dev

How to find the array indexes for each element in another array in MATLAB?

From Dev

how to find a element in a nested array and get its sub array index

From Dev

How to find neighbor element of array 1 from reverse of the same array

From Dev

MongoDB: find element by array element

From Dev

Mongoose find element in array

From Dev

Find the last element in an array

From Dev

Find an element from a array

Related Related

  1. 1

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

  2. 2

    How to find an element in an array in mongodb?

  3. 3

    How to find the largest element of an array of known size?

  4. 4

    How to find index of all occurrences of element in array?

  5. 5

    How to use XPath expression to find element in an array

  6. 6

    How to find number of element in an Array in java?

  7. 7

    how to find a particular string in an element of array in python

  8. 8

    How to find index of an Array element in OCaml

  9. 9

    How to find the last element in an under capacity array?

  10. 10

    How to find first instance of element array

  11. 11

    How to find the minimum and maximum element of the array?

  12. 12

    how to find an element in array of object with streams

  13. 13

    how to find already values in the array element

  14. 14

    How to use XPath expression to find element in an array

  15. 15

    How to find element in string(char array) and double it?

  16. 16

    how to find a[i]th element in array b

  17. 17

    How to I find a certain element in an array and change it?

  18. 18

    how to find middle element of array in javascript?

  19. 19

    ArangoDB - how find element in collection by substring of element of array?

  20. 20

    How to find the nearest larger element to another element in an array?

  21. 21

    how to find id of array element from element text/value

  22. 22

    How to find the next element given a certain element in array

  23. 23

    How to find the array indexes for each element in another array in MATLAB?

  24. 24

    how to find a element in a nested array and get its sub array index

  25. 25

    How to find neighbor element of array 1 from reverse of the same array

  26. 26

    MongoDB: find element by array element

  27. 27

    Mongoose find element in array

  28. 28

    Find the last element in an array

  29. 29

    Find an element from a array

HotTag

Archive