-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy patharithmetic_slices.py
More file actions
80 lines (64 loc) · 1.95 KB
/
arithmetic_slices.py
File metadata and controls
80 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
An integer array is called arithmetic if it
consists of at least three elements and if
the difference between any two consecutive
elements is the same.
Given an integer array nums,
return the number of
arithmetic subarrays of nums.
A subarray is a contiguous
subsequence of the array.
"""
class ArithmeticSlices:
def numberofarithmeticslices(self, nums):
"""
This defines a class and a function.
`nums` is input list of integers.
"""
n = len(nums)
"""
We store the length of the
array nums in variable n
"""
if n < 3:
return 0
total = 0
curr = 0
"""
An *arithmetic slice* must have **at least 3 numbers**.
So, if the array has fewer than 3 elements,
no arithmetic slices are possible — immediately return `0`.
"""
for i in range(2, n):
if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]:
curr += 1
total += curr
else:
curr = 0
"""
We start iterating from index `2`
because we need **three elements**
(`nums[i-2], nums[i-1], nums[i]`)
to check if they form an arithmetic pattern.
<<<<<<< HEAD
So at each step,
we are looking at a triplet ending at index `i`.
=======
So at each step,
we’re looking at a triplet ending at index `i`.
>>>>>>> ca34f0a649ef94c8b778a4babe040fcaa143a56e
"""
return total
"""
test_cases = [
# Basic cases
([1, 2, 3, 4], 3), # [1,2,3], [2,3,4], [1,2,3,4]
([1, 3, 5, 7, 9], 6), # all diffs = 2;
total slices = 6
# Edge cases
([1, 2], 0), # less than 3 elements → 0
([1, 1, 1], 1), # [1,1,1] itself is arithmetic
([1], 0), # single element
([], 0), # empty array
]
"""