Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of arithmetic sequences from a list of numbers in Python?
Finding arithmetic sequences from a list of numbers is a common problem in programming. An arithmetic sequence is a sequence where the difference between consecutive numbers remains constant. We need to count all contiguous arithmetic subsequences of length ? 3.
For example, in the list [6, 8, 10, 12, 13, 14], we have arithmetic sequences: [6, 8, 10], [8, 10, 12], [6, 8, 10, 12], and [12, 13, 14].
Algorithm Approach
The key insight is to use a sliding window approach:
Track consecutive elements that form arithmetic sequences
When a sequence breaks, calculate how many subsequences it contains
Use the formula: for n consecutive arithmetic elements, we get n×(n+1)/2 subsequences of length ? 3
Implementation
def count_arithmetic_sequences(nums):
if len(nums) < 3:
return 0
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
# Handle remaining sequence at the end
if count:
ans += (count * (count + 1)) // 2
return ans
# Test the function
nums = [6, 8, 10, 12, 13, 14]
result = count_arithmetic_sequences(nums)
print(f"Number of arithmetic sequences: {result}")
Number of arithmetic sequences: 4
How It Works
Let's trace through the example [6, 8, 10, 12, 13, 14]:
def count_arithmetic_sequences_with_trace(nums):
if len(nums) < 3:
return 0
count = 0
ans = 0
print(f"Processing: {nums}")
for i in range(2, len(nums)):
diff1 = nums[i] - nums[i - 1]
diff2 = nums[i - 1] - nums[i - 2]
print(f"i={i}: {nums[i-2]}, {nums[i-1]}, {nums[i]} | diff1={diff1}, diff2={diff2}")
if diff1 == diff2:
count += 1
print(f" Arithmetic continues, count={count}")
else:
if count > 0:
sequences = (count * (count + 1)) // 2
ans += sequences
print(f" Sequence breaks, adding {sequences} sequences, total={ans}")
count = 0
# Handle remaining sequence
if count:
sequences = (count * (count + 1)) // 2
ans += sequences
print(f"Final sequence adds {sequences} sequences, total={ans}")
return ans
nums = [6, 8, 10, 12, 13, 14]
result = count_arithmetic_sequences_with_trace(nums)
Processing: [6, 8, 10, 12, 13, 14] i=2: 6, 8, 10 | diff1=2, diff2=2 Arithmetic continues, count=1 i=3: 8, 10, 12 | diff1=2, diff2=2 Arithmetic continues, count=2 i=4: 10, 12, 13 | diff1=1, diff2=2 Sequence breaks, adding 3 sequences, total=3 i=5: 12, 13, 14 | diff1=1, diff2=1 Arithmetic continues, count=1 Final sequence adds 1 sequences, total=4
Multiple Test Cases
def test_arithmetic_sequences():
test_cases = [
([1, 2, 3, 4], 3), # [1,2,3], [2,3,4], [1,2,3,4]
([1, 3, 5, 7, 9], 6), # Multiple sequences
([1, 2, 4, 8], 0), # No arithmetic sequences
([5, 5, 5, 5], 3), # Constant difference of 0
]
for nums, expected in test_cases:
result = count_arithmetic_sequences(nums)
print(f"Input: {nums}")
print(f"Expected: {expected}, Got: {result}")
print(f"Status: {'?' if result == expected else '?'}")
print("-" * 40)
test_arithmetic_sequences()
Input: [1, 2, 3, 4] Expected: 3, Got: 3 Status: ? ---------------------------------------- Input: [1, 3, 5, 7, 9] Expected: 6, Got: 6 Status: ? ---------------------------------------- Input: [1, 2, 4, 8] Expected: 0, Got: 0 Status: ? ---------------------------------------- Input: [5, 5, 5, 5] Expected: 3, Got: 3 Status: ?
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass through the array |
| Space | O(1) | Only using constant extra space |
Conclusion
The algorithm efficiently counts arithmetic sequences using a sliding window approach with O(n) time complexity. The key insight is recognizing that n consecutive arithmetic elements generate n×(n+1)/2 valid subsequences of length ? 3.
