Skip to main content
  1. LeetCode/

LeetCode 413: Arithmetic Slices

·3 mins· ·
LeetCode Medium Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-05-10 Source: Day 21 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

    1. Explain why LC 413 adds previous streak plus one new length-3 slice.
  • LC 413 Arithmetic Slices: Pass.

Learning Note Extract
#

Problem 2 - LC 413 Arithmetic Slices
#

  • Status: Pass.
  • Pattern: 1D streak DP on contiguous subarrays.

Correct State
#

curr = number of arithmetic slices ending at the current index
total = total number of arithmetic slices seen so far

Why This State Fits
#

The problem is about:

contiguous subarrays

So at each index i, only the last 2 adjacent differences matter:

nums[i] - nums[i - 1]
nums[i - 1] - nums[i - 2]

If they match, then:

  • every arithmetic slice ending at i - 1 can extend to i
  • plus the last 3 elements form one new arithmetic slice

So:

curr += 1
total += curr

If the difference breaks:

curr = 0

Initialization
#

curr = total = 0

Why:

fewer than 3 elements cannot form an arithmetic slice

Complexity
#

Time: O(n)
Space: O(1)

Common Mistakes
#

  • confusing contiguous subarrays with subsequences
  • saying only the new length-3 slice matters and forgetting earlier slices can extend
  • using extra state that duplicates the rolling DP meaning

Interview-Ready Explanation
#

This is streak DP on contiguous subarrays. I define curr as the number of arithmetic slices ending at the current index, and total as the total number of arithmetic slices seen so far. Starting from index 2, if the last 2 adjacent differences are equal, then every arithmetic slice ending at i - 1 can extend to i, and the last 3 elements form one new slice, so I do curr += 1 and total += curr. Otherwise the streak breaks and curr = 0. The time complexity is O(n) and the space complexity is O(1).

Code
#

class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        total = 0
        curr = 0

        for i in range(2, len(nums)):
            if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]:
                curr += 1
                total += curr
            else:
                curr = 0

        return total

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        curr = total = 0
        for i in range(2, len(nums)):
            if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]:
                curr += 1
                total += curr
            else:
                curr = 0
        return total

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Counting only length-3 slices.
  • Not resetting when the difference changes.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 53: Maximum Subarray
·3 mins
LeetCode Medium Dynamic-Programming Kadane
LeetCode note for Maximum Subarray, rebuilt from the original learning note
LeetCode 983: Minimum Cost For Tickets
·3 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Minimum Cost For Tickets, rebuilt from the original learning note
LeetCode 152: Maximum Product Subarray
·3 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Maximum Product Subarray, rebuilt from the original learning note
LeetCode 300: Longest Increasing Subsequence
·3 mins
LeetCode Medium Dynamic-Programming Binary-Search
LeetCode note for Longest Increasing Subsequence, rebuilt from the original learning note
LeetCode 322: Coin Change
·2 mins
LeetCode Medium Dynamic-Programming Unbounded-Knapsack
LeetCode note for Coin Change, rebuilt from the original learning note
LeetCode 343: Integer Break
·3 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Integer Break, rebuilt from the original learning note