- Related Questions & Answers
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Program to check whether we can make k palindromes from given string characters or not in Python?
- C program to find the sum of arithmetic progression series
- Missing Number In Arithmetic Progression using C++
- C Program for N-th term of Arithmetic Progression series
- Program to check we can form array from pieces or not in Python
- Program to check whether we can reach last position from index 0 in Python
- Longest Arithmetic Sequence in C++
- Find the missing number in Arithmetic Progression in C++
- Program to check we can reach end of list by starting from k in Python
- Program to check we can visit any city from any city or not in Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Can Make Palindrome from Substring in Python
- Convert given array to Arithmetic Progression by adding an element in C++
- Program to count number of strings we can make using grammar rules in Python

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

Suppose we have a list of numbers called nums. We have to check whether the elements present in nums are forming AP series or not. As we know in AP (Arithmetic Progression) series the common difference between any two consecutive elements is the same.

So, if the input is like nums = [9,1,17,5,13], then the output will be True because if we sort them, it will be [1,5,9,13,17] and here common difference is 4 for each pair of elements.

To solve this, we will follow these steps −

nums := sort the list nums

if number of elements in nums > 1, then

const := nums[1] - nums[0]

otherwise,

return True

for i in range 0 to size of nums -1, do

if nums[i+1] - nums[i] is not same as const, then

return False

return True

Let us see the following implementation to get better understanding −

def solve(nums): nums = sorted(nums) if len(nums) > 1: const = nums[1] - nums[0] else: return True for i in range(len(nums)-1): if nums[i+1] - nums[i] != const: return False return True nums = [9,1,17,5,13] print(solve(nums))

[9,1,17,5,13]

True

Advertisements