Skip to content

Commit cf8d7bb

Browse files
committed
Update arithmetic_slices.py
2 parents 898b97f + ca34f0a commit cf8d7bb

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed
Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
11
"""
2-
An integer array is called arithmetic if it
3-
consists of at least three elements and if
4-
the difference between any two consecutive
5-
elements is the same.
2+
An integer array is called arithmetic if it
3+
consists of at least three elements and if
4+
the difference between any two consecutive
5+
elements is the same.
66
7-
Given an integer array nums,
8-
return the number of
9-
arithmetic subarrays of nums.
7+
Given an integer array nums,
8+
return the number of
9+
arithmetic subarrays of nums.
1010
11-
A subarray is a contiguous
12-
subsequence of the array.
11+
A subarray is a contiguous
12+
subsequence of the array.
1313
1414
"""
1515

16+
1617
class ArithmeticSlices:
1718
def numberofarithmeticslices(self, nums):
18-
1919
"""
20-
This defines a class and a function.
20+
This defines a class and a function.
2121
`nums` is input list of integers.
2222
"""
2323
n = len(nums)
24-
24+
2525
"""
26-
We store the length of the
26+
We store the length of the
2727
array nums in variable n
2828
"""
2929
if n < 3:
3030
return 0
31-
31+
3232
total = 0
3333
curr = 0
34-
34+
3535
"""
3636
An *arithmetic slice* must have **at least 3 numbers**.
3737
38-
So, if the array has fewer than 3 elements,
38+
So, if the array has fewer than 3 elements,
3939
no arithmetic slices are possible — immediately return `0`.
4040
"""
41-
41+
4242
for i in range(2, n):
4343
if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]:
4444
curr += 1
4545
total += curr
4646
else:
4747
curr = 0
48-
48+
4949
"""
50-
We start iterating from index `2`
51-
because we need **three elements**
52-
(`nums[i-2], nums[i-1], nums[i]`)
50+
We start iterating from index `2`
51+
because we need **three elements**
52+
(`nums[i-2], nums[i-1], nums[i]`)
5353
to check if they form an arithmetic pattern.
5454
55+
<<<<<<< HEAD
5556
So at each step,
5657
we are looking at a triplet ending at index `i`.
58+
=======
59+
So at each step,
60+
we’re looking at a triplet ending at index `i`.
61+
>>>>>>> ca34f0a649ef94c8b778a4babe040fcaa143a56e
5762
"""
58-
63+
5964
return total
6065

66+
6167
"""
6268
test_cases = [
6369
# Basic cases
6470
([1, 2, 3, 4], 3), # [1,2,3], [2,3,4], [1,2,3,4]
65-
([1, 3, 5, 7, 9], 6), # all diffs = 2;
71+
([1, 3, 5, 7, 9], 6), # all diffs = 2;
6672
total slices = 6
67-
73+
6874
# Edge cases
6975
([1, 2], 0), # less than 3 elements → 0
7076
([1, 1, 1], 1), # [1,1,1] itself is arithmetic
7177
([1], 0), # single element
7278
([], 0), # empty array
7379
]
74-
"""
80+
"""

0 commit comments

Comments
 (0)