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 check we can make arithmetic progression from sequence in Python
Suppose we have a list of numbers. We need to check whether these numbers can form an Arithmetic Progression (AP) when arranged in proper order. In an AP 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.
Algorithm
To solve this, we will follow these steps:
Sort the list nums in ascending order
If the list has more than 1 element, calculate the common difference from first two elements
If the list has 0 or 1 element, return True (trivially forms AP)
Check if all consecutive pairs have the same difference
Return False if any pair has different difference, otherwise return True
Example
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("Original list:", nums)
print("Can form AP:", solve(nums))
print("Sorted list:", sorted(nums))
Original list: [9, 1, 17, 5, 13] Can form AP: True Sorted list: [1, 5, 9, 13, 17]
Testing with Different Cases
Let's test our solution with various scenarios:
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
# Test cases
test_cases = [
[9, 1, 17, 5, 13], # Can form AP: [1,5,9,13,17] with diff=4
[1, 2, 4, 8], # Cannot form AP
[5], # Single element - trivially AP
[3, 3, 3, 3], # All same elements with diff=0
[10, 5, 15, 20, 25] # Can form AP: [5,10,15,20,25] with diff=5
]
for i, nums in enumerate(test_cases):
result = solve(nums)
print(f"Test {i+1}: {nums} ? {result}")
Test 1: [9, 1, 17, 5, 13] ? True Test 2: [1, 2, 4, 8] ? False Test 3: [5] ? True Test 4: [3, 3, 3, 3] ? True Test 5: [10, 5, 15, 20, 25] ? True
How It Works
The algorithm works by first sorting the array to arrange elements in ascending order. Then it calculates the expected common difference from the first two elements. Finally, it verifies that all consecutive pairs maintain this same difference.
The time complexity is O(n log n) due to sorting, and space complexity is O(1) if we modify the input in-place or O(n) for the sorted copy.
Conclusion
This solution efficiently determines if a sequence can form an arithmetic progression by sorting and checking consecutive differences. The approach handles edge cases like single elements and ensures all pairs maintain the same common difference.
