Skip to content

Commit 8eb7dfe

Browse files
Can Make Arithmetic Progression From Sequence
Difficulty: Easy 102 / 102 test cases passed. Runtime: 36 ms Memory Usage: 14.2 MB
1 parent 1645543 commit 8eb7dfe

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Given an array of numbers arr. A sequence of numbers is called an
3+
arithmetic progression if the difference between any two consecutive
4+
elements is the same.
5+
Return true if the array can be rearranged to form an arithmetic
6+
progression, otherwise, return false.
7+
8+
Example:
9+
Input: arr = [3,5,1]
10+
Output: true
11+
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with
12+
ifferences 2 and -2 respectively, between each consecutive
13+
elements.
14+
15+
Example:
16+
Input: arr = [1,2,4]
17+
Output: false
18+
Explanation: There is no way to reorder the elements to obtain an
19+
arithmetic progression.
20+
21+
Constraints:
22+
- 2 <= arr.length <= 1000
23+
- -10^6 <= arr[i] <= 10^6
24+
"""
25+
#Difficulty: Easy
26+
#102 / 102 test cases passed.
27+
#Runtime: 36 ms
28+
#Memory Usage: 14.2 MB
29+
30+
#Runtime: 36 ms, faster than 90.39% of Python3 online submissions for Can Make Arithmetic Progression From Sequence.
31+
#Memory Usage: 14.2 MB, less than 6.83% of Python3 online submissions for Can Make Arithmetic Progression From Sequence.
32+
33+
class Solution:
34+
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
35+
arr.sort()
36+
diff = arr[1] - arr[0]
37+
for i in range(1, len(arr)):
38+
if arr[i] - arr[i-1] != diff:
39+
return False
40+
return True

0 commit comments

Comments
 (0)