Count Subarrays Where Max Element Appears at Least K Times

You are given an integer array `nums` and a **positive** integer `k`. Return *the number of subarrays where the **maximum** element of* `nums` *appears **at least*** `k` *times in that subarray.* A **subarray** is a contiguous sequence of elements within an array.

30:00

Count Subarrays Where Max Element Appears at Least K Times
medium
Free written solution
Topics
Companies

You are given an integer array nums and a positive integer k.

Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.

A subarray is a contiguous sequence of elements within an array.

Example 1:
Input: {"nums":[1,3,2,3,3],"k":2}
Output: 6
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 1≤nums[i]≤1061 \leq \text{nums}[i] \leq 10^6

  • 1≤k≤1051 \leq k \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,3,2,3,3],"k":2}

The maximum of the whole array is 3 and it appears 3 times. Only copies of 3 matter.

nums · max = 3

▲

1

▲

3

▲

2

▲

3

▲

3

0

1

2

3

4

copies of 3 in window: 0 (need 2)

left

0

added

+0

total

0

Variables
VariableValue
mx3
DepthFunction Call
Stack empty
0/16