Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
[1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.Given an integer array nums, return the number of arithmetic subarrays of nums.
A subarray is a contiguous subsequence of the array.
Example 1:
Input: nums = [1,2,3,4] Output: 3 Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
Example 2:
Input: nums = [1] Output: 0
Constraints:
1 <= nums.length <= 5000-1000 <= nums[i] <= 1000Problem summary: 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. For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences. Given an integer array nums, return the number of arithmetic subarrays of nums. A subarray is a contiguous subsequence of the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Sliding Window
[1,2,3,4]
[1]
arithmetic-slices-ii-subsequence)arithmetic-subarrays)number-of-zero-filled-subarrays)length-of-the-longest-alphabetical-continuous-substring)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #413: Arithmetic Slices
class Solution {
public int numberOfArithmeticSlices(int[] nums) {
int ans = 0, cnt = 0;
int d = 3000;
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i + 1] - nums[i] == d) {
++cnt;
} else {
d = nums[i + 1] - nums[i];
cnt = 0;
}
ans += cnt;
}
return ans;
}
}
// Accepted solution for LeetCode #413: Arithmetic Slices
func numberOfArithmeticSlices(nums []int) (ans int) {
cnt, d := 0, 3000
for i, b := range nums[1:] {
a := nums[i]
if b-a == d {
cnt++
} else {
d = b - a
cnt = 0
}
ans += cnt
}
return
}
# Accepted solution for LeetCode #413: Arithmetic Slices
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
ans = cnt = 0
d = 3000
for a, b in pairwise(nums):
if b - a == d:
cnt += 1
else:
d = b - a
cnt = 0
ans += cnt
return ans
// Accepted solution for LeetCode #413: Arithmetic Slices
struct Solution;
impl Solution {
fn number_of_arithmetic_slices(a: Vec<i32>) -> i32 {
let mut slice = vec![];
let mut res = 0;
for x in a {
if slice.len() < 2 {
slice.push(x);
} else {
let y = slice.pop().unwrap();
let z = slice.pop().unwrap();
if y - z == x - y {
slice.push(z);
slice.push(y);
slice.push(x);
res += slice.len() - 2;
} else {
slice.clear();
slice.push(y);
slice.push(x);
}
}
}
res as i32
}
}
#[test]
fn test() {
let a = vec![1, 2, 3, 4];
let res = 3;
assert_eq!(Solution::number_of_arithmetic_slices(a), res);
}
// Accepted solution for LeetCode #413: Arithmetic Slices
function numberOfArithmeticSlices(nums: number[]): number {
let ans = 0;
let cnt = 0;
let d = 3000;
for (let i = 0; i < nums.length - 1; ++i) {
const a = nums[i];
const b = nums[i + 1];
if (b - a == d) {
++cnt;
} else {
d = b - a;
cnt = 0;
}
ans += cnt;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.