Skip to main content
LeetCode 795, Medium. Topics: Array, Two Pointers. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 23 parametrized pytest cases, and a playground notebook:

Problem

Given an integer array <code>nums</code> and two integers <code>left</code> and <code>right</code>, return <em>the number of contiguous non-empty <strong>subarrays</strong> such that the value of the maximum array element in that subarray is in the range</em> <code>[left, right]</code>. The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.

Examples

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • 0 <= left <= right <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026