- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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[0] is same as nums[1], then/p>

return nums[0]

lower := nums[0]

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[0] + interval * pointer, then

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

return nums[0] + 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[0] == nums[1]: return nums[0] lower = nums[0] 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[0] + interval * pointer: if nums[pointer - 1] == nums[0] + interval * (pointer -1): return nums[0] + 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

- Related Articles
- Program to check we can make arithmetic progression from sequence in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Program to find nth term in Look and Say Sequence in Python
- Program to check how many queries finds valid arithmetic sequence in Python
- Program to find maximum k-repeating substring from sequence in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Longest Arithmetic Sequence in C++
- Program to find nth Fibonacci term in Python
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Program to find nth sequence after following the given string sequence rules in Python
- Program to find length of longest consecutive sequence in Python
- Program to find shortest subarray to be removed to make array sorted in Python