2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution

In this guide, you will get 2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution with the best time and space complexity. The solution to Count Subarrays Where Max Element Appears at Least K Times problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Subarrays Where Max Element Appears at Least K Times solution in C++
  4. Count Subarrays Where Max Element Appears at Least K Times solution in Java
  5. Count Subarrays Where Max Element Appears at Least K Times solution in Python
  6. Additional Resources
2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: nums = [1,3,2,3,3], k = 2
Output: 6
Explanation: The subarrays that contain the element 3 at least 2 times 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 = [1,4,2,1], k = 3
Output: 0
Explanation: No subarray contains the element 4 at least 3 times.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 106
1 <= k <= 105

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution in C++

class Solution {
 public:
  long long countSubarrays(vector<int>& nums, int k) {
    const int maxNum = ranges::max(nums);
    long ans = 0;
    int count = 0;

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      if (nums[r] == maxNum)
        ++count;
      // Keep the window to include k - 1 times of the maximum number.
      while (count == k)
        if (nums[l++] == maxNum)
          --count;
      // If l > 0, nums[l..r] has k - 1 times of the maximum number. For any
      // subarray nums[i..r], where i < l, it will have at least k times of the
      // maximum number, since nums[l - 1] equals the maximum number.
      ans += l;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution in Java

class Solution {
  public long countSubarrays(int[] nums, int k) {
    final int maxNum = Arrays.stream(nums).max().getAsInt();
    long ans = 0;
    int count = 0;

    for (int l = 0, r = 0; r < nums.length; ++r) {
      if (nums[r] == maxNum)
        ++count;
      // Keep the window to include k - 1 times of the maximum number.
      while (count == k)
        if (nums[l++] == maxNum)
          --count;
      // If l > 0, nums[l..r] has k - 1 times of the maximum number. For any
      // subarray nums[i..r], where i < l, it will have at least k times of the
      // maximum number, since nums[l - 1] equals the maximum number.
      ans += l;
    }

    return ans;
  }
}
// code provided by PROGIEZ

2962. Count Subarrays Where Max Element Appears at Least K Times LeetCode Solution in Python

class Solution:
  def countSubarrays(self, nums: list[int], k: int) -> int:
    maxNum = max(nums)
    ans = 0
    count = 0

    l = 0
    for r, num in enumerate(nums):
      if num == maxNum:
        count += 1
      # Keep the window to include k - 1 times of the maxNummum number.
      while count == k:
        if nums[l] == maxNum:
          count -= 1
        l += 1
      # If l > 0, nums[l:r+1] has k - 1 times of the maxNummum number. For any
      # subarray nums[i:r+1], where i < l, it will have at least k times of the
      # maxNummum number, since nums[l - 1] equals the maxNummum number.
      ans += l

    return ans
# code by PROGIEZ

Additional Resources

See also  1237. Find Positive Integer Solution for a Given Equation LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.