Skip to content

Commit 825ae7d

Browse files
committed
Runtime: 194 ms (Top 22.81%) | Memory: 17.70 MB (Top 6.09%)
1 parent 2dfbe8b commit 825ae7d

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
1+
// Runtime: 194 ms (Top 22.81%) | Memory: 17.70 MB (Top 6.09%)
2+
13
class Solution:
24
def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:
3-
out=[]
4-
for i, j in zip(l, r):
5-
out.append(self.canMakeArithmeticProgression(nums[i:j+1]))
6-
return out
5+
ans = []
76

8-
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
9-
minArr = min(arr)
10-
maxArr = max(arr)
11-
12-
# if difference between minArr and maxArr cannot be divided into equal differences, then return false
13-
if (maxArr-minArr)%(len(arr)-1)!=0:
14-
return False
15-
16-
# consecutive difference in arithmetic progression
17-
diff = int((maxArr-minArr)/(len(arr)-1))
18-
if diff == 0:
19-
if arr != [arr[0]]*len(arr):
20-
return False
21-
return True
22-
23-
# array to check all numbers in A.P. are present in input array.
24-
# A.P.[minArr, minArr+d, minArr+2d, . . . . . . . maxArr]
25-
check = [1]*len(arr)
26-
for num in arr:
27-
if (num-minArr)%diff != 0:
28-
return False
29-
check[(num-minArr)//diff]=0
30-
31-
# if 1 is still in check array it means at least one number from A.P. is missing from input array.
32-
if 1 in check:
33-
return False
34-
return True
7+
def find_diffs(arr):
8+
9+
arr.sort()
10+
11+
dif = []
12+
13+
for i in range(len(arr) - 1):
14+
dif.append(arr[i] - arr[i + 1])
15+
16+
return len(set(dif)) == 1
17+
18+
for i , j in zip(l , r):
19+
ans.append(find_diffs(nums[i:j + 1]))
20+
21+
return ans

0 commit comments

Comments
 (0)