# Program to find removed term from arithmetic sequence in Python

Suppose we have an array called nums holding n-1 arithmetic sequence terms. One element except the first or last element of nums was removed before. We have to find the removed number.

So, if the input is like nums = [5, 7, 11, 13], then the output will be 9 because, the items are following the formula 2i+5, so for i = 2 it will be 2*2 + 5 = 9 which is missing.

To solve this, we will follow these steps −

• if size of nums is same as 2, then

• return floor of (sum of all elements present in nums)/2

• if nums is same as nums, then/p>

• return nums

• lower := nums

• upper := last element of nums

• interval := floor of (upper - lower) / size of nums

• pointer := floor of size of nums / 2

• left := 0

• right := size of nums - 1

• while left is not same as right, do

• if nums[pointer] is not same as nums + interval * pointer, then

• if nums[pointer - 1] is same as nums + interval *(pointer - 1) , then

• return nums + interval * pointer

• otherwise,

• right := pointer

• pointer :=floor of(left + right) / 2

• otherwise,

• if right - left is same as 1, then

• pointer := right

• otherwise,

• left := pointer

• pointer := floor of(left + right) / 2

## Example

Let us see the following implementation to get better understanding

def solve(nums):
if len(nums) == 2:
return sum(nums) // 2

if nums == nums:
return nums

lower = nums
upper = nums[-1]
interval = (upper - lower) // len(nums)

pointer = len(nums) // 2

left = 0
right = len(nums) - 1

while left != right:
if nums[pointer] != nums + interval * pointer:
if nums[pointer - 1] == nums + interval * (pointer -1):
return nums + interval * pointer
else:
right = pointer
pointer = (left + right) // 2
else:
if right - left == 1:
pointer = right
else:
left = pointer
pointer = (left + right) // 2

nums = [5, 7, 11, 13]
print(solve(nums))

## Input

[5, 7, 11, 13]

## Output

9