01. Sum of Mode
β GFG solution to the Sum of Mode problem: find sum of modes of all subarrays of size k using efficient sliding window with frequency buckets technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[] of positive integers and an integer k. Your task is to find the sum of the modes of all the subarrays of size k.
Note: The mode of a subarray is the element that occurs with the highest frequency. If multiple elements have the same highest frequency, the smallest such element is considered the mode.
π Examples
Example 1
Input: arr[] = [1, 2, 3, 2, 5, 2, 4, 4], k = 3
Output: 13
Explanation: The subarrays of size 3 are:
[1, 2, 3] β mode = 1 (all have frequency 1, smallest is 1)
[2, 3, 2] β mode = 2 (frequency 2)
[3, 2, 5] β mode = 2 (all have frequency 1, smallest is 2)
[2, 5, 2] β mode = 2 (frequency 2)
[5, 2, 4] β mode = 2 (all have frequency 1, smallest is 2)
[2, 4, 4] β mode = 4 (frequency 2, but 2 also has frequency 1, so smallest among max freq is 4)
Actually: [2, 4, 4] β mode = 4 (frequency 2 for element 4)
Sum = 1 + 2 + 2 + 2 + 2 + 4 = 13Example 2
π Constraints
$1 \le k \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^5$
β
My Approach
The optimal approach uses Sliding Window technique with Frequency Buckets to efficiently track modes across all subarrays:
Sliding Window + Frequency Buckets
Data Structures:
freq: Hash map to store frequency of each element in current window.buckets: Map where key is frequency and value is set of elements with that frequency.
Initial Window Setup:
Process first
kelements to populate frequency map.Organize elements by their frequencies in buckets.
Mode is the smallest element from the bucket with highest frequency.
Sliding Window Process:
Remove Element: When sliding window, remove
arr[i-k]from current window:Remove from its current frequency bucket
Decrease its frequency
Add to new frequency bucket if frequency > 0
Add Element: Add
arr[i]to current window:Remove from its current frequency bucket (if exists)
Increase its frequency
Add to new frequency bucket
Mode Calculation:
After each window update, find the bucket with maximum frequency.
From that bucket, select the smallest element as mode.
Add this mode to running sum.
Efficiency:
Bucket structure allows O(log k) access to maximum frequency.
Set within each bucket allows O(log k) access to smallest element with that frequency.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log k), where n is the size of the array and k is the window size. Each element is processed with map/set operations that take O(log k) time.
Expected Auxiliary Space Complexity: O(k), where k is the window size. We maintain frequency map and buckets that together store at most k distinct elements and their frequency distributions.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated