I ran into this problem when you were assigned a number Nas Input, and then N numbers followed (where 3 <= N <= 2500). These numbers Nwere part of the arithmetic progression (size N+1) from which one number was removed. Therefore, the task was to find this missing number. For example,
5
1 3 5 9 11
Output signal 7
I came up with two methods, the second - all test cases, but the first one does not work in certain (hidden) cases.
I will explain the second method first
METHOD II
Let diff=(last_number-first_number)/N
for i=0 to (N-2)
if( array[i+1] is not equal to (array[i]+diff))
print (array[i]+diff)
break
This method has passed all test cases. Now, the first method that I performed, and which failed some test cases, was as follows:
METHOD I
for i=1 to (N-2)
if (2*array[i] is not equal to (array[i-1]+array[i+1])) then
if( (array[i]-array[i-1])< (array[i+1]-array[i]))
print 2*array[i]-array[i-1]
else
print 2*array[i]-array[i+1]
break
- , METHOD I? .
.