Tap here to switch tabs
Problem
Submissions

Problem: Sum of Weighted Modes in Subarrays

med
30 min
Try to solve the Sum of Weighted Modes in Subarrays problem.

Statement

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

The mode of a subarray is the element with the highest frequency in that subarray. If multiple elements share the maximum frequency, the mode is the smallest element among them.

The weight of a subarray is calculated as the product of its mode and the frequency of that mode within the subarray.

Return the total sum of the weights across all contiguous subarrays of length k.

Constraints:

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

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

  • 1≤1 \leq k ≤\leq nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sum of Weighted Modes in Subarrays

med
30 min
Try to solve the Sum of Weighted Modes in Subarrays problem.

Statement

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

The mode of a subarray is the element with the highest frequency in that subarray. If multiple elements share the maximum frequency, the mode is the smallest element among them.

The weight of a subarray is calculated as the product of its mode and the frequency of that mode within the subarray.

Return the total sum of the weights across all contiguous subarrays of length k.

Constraints:

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

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

  • 1≤1 \leq k ≤\leq nums.length