Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Google
Count Subarrays Where Max Element Appears At Least K Times

Count the number of contiguous subarrays in which the maximum value of the whole array appears at least k times.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums and an integer k, count how many contiguous subarrays contain the maximum element of nums at least k times.

A subarray is a non-empty contiguous segment of the array.

You need to return the total number of such subarrays.

Clarification

The maximum element refers to the largest value present anywhere in the entire array, not the maximum of each subarray.

Input Format

  • An integer array nums
  • An integer k

The exact platform function signature may vary, but the logical inputs are the same.

Output Format

Return an integer representing the number of contiguous subarrays whose global maximum element appears at least k times.

Constraints

  • 1 <= nums.length
  • 1 <= k
  • nums[i] are integers
  • The answer may be larger than 32-bit signed integer range, so use a 64-bit integer type if needed.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,3,2,3,3], k = 2

Output

6

Explanation

The global maximum is 3. The subarrays with at least two 3s are [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3], and [3,3].

Example 2

Input

nums = [5,5,5], k = 3

Output

1

Explanation

Only the whole array [5,5,5] contains the maximum value 3 times.

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.