Tap here to switch tabs
Problem
Submissions

Problem: Valid Subarrays With Exactly One Peak

Valid Subarrays With Exactly One Peak asks you to count contiguous array ranges containing exactly one index greater than both neighbors, with that peak no more than k positions from either boundary.

Statement

You are given an integer array nums of length nn and an integer k.

An index p (where 0<p<n−10 < p < n - 1) is considered a peak if nums[p] > nums[p - 1] and nums[p] > nums[p + 1].

A subarray starting at index l and ending at index r (where 0≤l≤r<n0 \leq l \leq r < n) is considered valid if:

  1. It contains exactly 11 peak index p.

  2. The distance from the peak p to each boundary is at most k, meaning p−l≤kp - l \leq k and r−p≤kr - p \leq k.

Return the total number of valid subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • −105≤-10^5 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq k ≤n\leq n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Valid Subarrays With Exactly One Peak

Valid Subarrays With Exactly One Peak asks you to count contiguous array ranges containing exactly one index greater than both neighbors, with that peak no more than k positions from either boundary.

Statement

You are given an integer array nums of length nn and an integer k.

An index p (where 0<p<n−10 < p < n - 1) is considered a peak if nums[p] > nums[p - 1] and nums[p] > nums[p + 1].

A subarray starting at index l and ending at index r (where 0≤l≤r<n0 \leq l \leq r < n) is considered valid if:

  1. It contains exactly 11 peak index p.

  2. The distance from the peak p to each boundary is at most k, meaning p−l≤kp - l \leq k and r−p≤kr - p \leq k.

Return the total number of valid subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • −105≤-10^5 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq k ≤n\leq n