Skip to content

Practice Continuous Subarrays

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

Count nonempty contiguous subarrays of nums in which the absolute difference between every pair of values is at most 2. Equivalently, the largest and smallest values within each counted subarray differ by at most 2. Return the count.

Source: Continuous Subarrays on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [5,4,2,4]
Output
8

# Example 2

Input
nums = [1,2,3]
Output
6

Constraints

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Continuous Subarrays.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.