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+
1617class 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- So at each step,
55+ So at each step,
5656 we’re looking at a triplet ending at index `i`.
5757 """
58-
58+
5959 return total
6060
61+
6162"""
6263test_cases = [
6364 # Basic cases
6465 ([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;
66+ ([1, 3, 5, 7, 9], 6), # all diffs = 2;
6667 total slices = 6
67-
68+
6869 # Edge cases
6970 ([1, 2], 0), # less than 3 elements → 0
7071 ([1, 1, 1], 1), # [1,1,1] itself is arithmetic
7172 ([1], 0), # single element
7273 ([], 0), # empty array
7374 ]
74- """
75+ """
0 commit comments