Count Contiguous Subarrays With a Constant Difference
Quick Overview
Practice counting contiguous arithmetic subarrays of length at least three. Include overlapping intervals, equal values, decreasing values, and breaks in the common difference.
Count Contiguous Subarrays With a Constant Difference
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
# Count Contiguous Subarrays With a Constant Difference
Given an integer array `nums`, count its contiguous subarrays of length at least three whose adjacent differences are all equal.
A difference may be positive, negative, or zero. Count subarrays by their start and end positions, so overlapping subarrays are counted separately.
### Function
`int numberOfArithmeticSlices(vector<int>& nums)`
### Example 1
```text
Input: nums = [6,3,0,-3,7]
Output: 3
```
The qualifying subarrays are `[6,3,0]`, `[3,0,-3]`, and `[6,3,0,-3]`. Including the final `7` breaks the common difference.
### Example 2
```text
Input: nums = [5,5,5,5,5]
Output: 6
```
There are three length-three, two length-four, and one length-five qualifying subarrays.
### Constraints
- `1 <= nums.length <= 5000`.
- `-1000 <= nums[i] <= 1000`.
### Clarifications
Do not count subsequences that skip elements. Length-one and length-two subarrays do not qualify, even though a two-element pair has one difference. Identical value sequences occurring at different positions count separately.
### What a Strong Answer Covers
- Minimum length, contiguity, and overlapping subarrays.
- Negative or zero differences and a break in an otherwise regular run.
- Why every qualifying pair of endpoints is counted exactly once.
- Correctness, count bounds, and resource usage.
Overview: Practice counting contiguous arithmetic subarrays of length at least three. Include overlapping intervals, equal values, decreasing values, and breaks in the common difference.
Count Contiguous Subarrays With a Constant Difference
Meta
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Count Contiguous Subarrays With a Constant Difference
Given an integer array nums, count its contiguous subarrays of length at least three whose adjacent differences are all equal.
A difference may be positive, negative, or zero. Count subarrays by their start and end positions, so overlapping subarrays are counted separately.
Function
int numberOfArithmeticSlices(vector<int>& nums)
Example 1
Input: nums = [6,3,0,-3,7]
Output: 3
The qualifying subarrays are [6,3,0], [3,0,-3], and [6,3,0,-3]. Including the final 7 breaks the common difference.
Example 2
Input: nums = [5,5,5,5,5]
Output: 6
There are three length-three, two length-four, and one length-five qualifying subarrays.
Constraints
1 <= nums.length <= 5000
.
-1000 <= nums[i] <= 1000
.
Clarifications
Do not count subsequences that skip elements. Length-one and length-two subarrays do not qualify, even though a two-element pair has one difference. Identical value sequences occurring at different positions count separately.
What a Strong Answer Covers Guidance
Minimum length, contiguity, and overlapping subarrays.
Negative or zero differences and a break in an otherwise regular run.
Why every qualifying pair of endpoints is counted exactly once.