- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 find the number of contiguous arithmetic sequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next number is the same.

So, if the input is like nums = [6, 8, 10, 12, 13, 14], then the output will be 4, as we have the arithmetic sequences like: [6, 8, 10] [8, 10, 12] [6, 8, 10, 12] [12, 13, 14]

To solve this, we will follow these steps −

count := 0, ans := 0

for i in range 2 to size of nums, do

if nums[i] - nums[i - 1] is same as nums[i - 1] - nums[i - 2], then

count := count + 1

otherwise,

ans := ans + quotient of (count * (count + 1)) / 2

count := 0

if count is non-zero, then

ans := ans + quotient of (count *(count + 1)) / 2

return ans

Let us see the following implementation to get better understanding:

class Solution: def solve(self, nums): count = 0 ans = 0 for i in range(2, len(nums)): if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]: count += 1 else: ans += (count * (count + 1)) // 2 count = 0 if count: ans += (count * (count + 1)) // 2 return ans ob = Solution() nums = [6, 8, 10, 12, 13, 14] print(ob.solve(nums))

[6, 8, 10, 12, 13, 14]

4

- Related Questions & Answers
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find missing numbers from two list of numbers in Python
- Program to find length of longest arithmetic subsequence of a given list in Python
- Program to find local peak element indices from a list of numbers in Python
- Program to find length of longest sign alternating subsequence from a list of numbers in Python
- Program to find largest distance pair from two list of numbers in Python
- Program to find the kth missing number from a list of elements in Python
- Python - Largest number possible from list of given numbers
- Program to find number of strictly increasing colorful candle sequences are there in Python
- Program to create largest lexicographic number from a list of numbers in C++
- How to find the greatest number in a list of numbers in Python?
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Program to find number of unique people from list of contact mail ids in Python
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- How to find sum a list of numbers in Python?

Advertisements