A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequence:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic. 1, 1, 2, 5, 7 


A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 

A slice (P, Q) of array A is called arithmetic if the sequence:
    A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A. 


Example:

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

Difficulty: interview

Starter code:

class Solution:
    def numberOfArithmeticSlices(self, A: List[int]) -> int:
        

Solutions:
class Solution:
     def numberOfArithmeticSlices(self, A):
         curr, sum = 0, 0
         for i in range(2,len(A)):
             if A[i]-A[i-1] == A[i-1]-A[i-2]:
                 curr += 1
                 sum += curr
             else:
                 curr = 0
         return sum
 #         solution = 0
 #         connected = 1
 #         old_diff = None
 #         sequences = []
 #         if len(A) < 3:
 #             return 0
         
 #         for index,num in enumerate(A):
 #             if index < len(A) - 1:
 #                 new_diff = num - A[index + 1]
 #             else:
 #                 new_diff = A[index - 1] - num
 #             if old_diff == new_diff:
 #                 if index == len(A) - 1 and connected >= 3:
 #                     connected += 1
 #                     sequences.append(connected)
 #                 connected += 1
 #             else:
 #                 old_diff = new_diff
 #                 if connected > 2:
 #                     sequences.append(connected)
 #                 connected = 1
 #         for sequence in sequences:
 #             prev = 0
 #             while sequence >= 2:
 #                 prev += 1
 #                 solution += prev
 #                 sequence -= 1
 #         return solution


class Solution:
     def sums(self, n):
         return int((n-2)*(n-1)/2)
 
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         if len(A) < 3:
             return 0
         res = []
         cnt = 2
         diff = 0
         i = 1
         while i < len(A)-1:
             if i == 1:
                 diff = A[i] - A[0]
             while i < len(A) - 1 and A[i+1] - A[i] == diff:
                 cnt += 1
                 i += 1
             if cnt >= 3:
                 res.append(cnt)
             if i < len(A) - 1:
                 diff = A[i+1] - A[i]
                 cnt = 2
                 i += 1
         return sum(self.sums(x) for x in res)


class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         dp ,counts = [0,0], 1
         for i in range(2,len(A)) :
             if A[i] - A[i - 1] == A[i - 1] - A[i - 2] :
                 dp.append(dp[i - 1] + counts)
                 counts += 1
             else :
                 dp.append(dp[i - 1])
                 counts = 1
         
         return dp[-1]
             


class Solution:
 	def numberOfArithmeticSlices(self, A):
 		ans = [0] * len(A)
 		for i in range(0, len(A)):
 			if i < 2:
 				ans[i] = 0
 			else:
 				# add number of slices for i - 1
 				ans[i] = ans[i - 1]
 				temp = A[:i + 1][::-1]
 				for j in range(0, i - 1):
 					slice = temp[j:j+3]
 					if self.checkArithmetic(slice):
 						ans[i] += 1
 					else:
 						break
 		return 0 if len(A) == 0 else ans[-1]
 		
 	def checkArithmetic(self, slice):
 		diff = slice[1] - slice[0]
 		for i in range(len(slice) - 1):
 			if slice[i+1] - slice[i] != diff:
 				return False
 		return True

class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         count=0
         addend=0
         for i in range(2,len(A)):
             if A[i-1]-A[i]==A[i-2]-A[i-1]:
                 addend+=1
                 count+=addend
             else:
                 addend=0
         return count

class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         
         if len(A) < 3:
             return 0
         
         count = 0
         l = len(A)
         
         i = 0
         while i < l-2:
             j = i + 2
             while j < l:
                 if A[i]-A[i+1] == A[j-1]-A[j]:
                     count += j-i-1
                     j += 1
                 else:
                     i = j-1
                     break
             if j == l:
                 break
 
         return count

class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         res = 0
         dp = [0] * len(A)
         for i in range(2, len(A)):
             if A[i] - A[i - 1] == A[i - 1] - A[i - 2]:
                 dp[i] = dp[i - 1] + 1
                 res += dp[i]
         return res

class Solution:
     def numberOfArithmeticSlices(self, A):
         curr, sum = 0, 0
         for i in range(2, len(A)):
             if A[i] - A[i - 1] == A[i - 1] - A[i - 2]:
                 curr += 1
                 sum += curr
             else:
                 curr = 0
         return sum

class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         size = len(A)
         if size < 3:
             return 0
         ans = 0
         cnt = 0
         delta = A[1] - A[0]
         
         for x in range(2, size):
             if A[x] - A[x-1] == delta:
                 cnt += 1
                 ans += cnt
             else:
                 delta = A[x] - A[x-1]
                 cnt = 0
         return ans

class Solution:
     def __init__(self):
       self.total_sum = 0
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         self.slices(A, len(A) - 1)
         return self.total_sum
         
     def slices(self, A, i):
         if i < 2:
           return 0
         temp = 0
         if A[i] - A[i-1] == A[i-1] - A[i-2]:
           temp = 1 + self.slices(A, i-1)
           self.total_sum += temp
         else:
           self.slices(A, i-1)
         return temp

class Solution:
     def numberOfArithmeticSlices(self, A):
         """
         :type A: List[int]
         :rtype: int
         """
         dp = [0 for _ in range(len(A))]
         for i in range(2, len(A)):
             if A[i] - A[i-1] == A[i-1] - A[i-2]:
                 dp[i] = dp[i-1] + 1
             else:
                 dp[i] = 0
                 
         return sum(dp)