Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Count Subarrays of Length Three With a Condition

Count how many contiguous subarrays of length three satisfy a given condition.

Acceptance 0%
Problem Statement

You are given an integer array nums. Consider every contiguous subarray of length 3.

Count how many of these length-3 subarrays satisfy the required condition from the problem statement. Return the count.

The exact condition is simple and local to each triplet, so the intended solution is to scan the array once and evaluate each window of size three.

Input Format

  • An integer array nums.
  • You may assume nums contains at least 3 elements for valid windows to exist.

Output Format

  • Return an integer: the number of length-3 contiguous subarrays that satisfy the condition.

Constraints

  • 3 <= nums.length
  • Elements are integers.
  • Time-efficient solutions should inspect each length-3 window once.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3, 4, 5]

Output

3

Explanation

The length-3 windows are [1,2,3], [2,3,4], and [3,4,5]. For this illustrative version, each window satisfies the local condition, so the count is 3.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.