Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int longestSubarray(const vector<int>& salesData, int frequencyThreshold) {
- unordered_map<int, int> freqMap; // Frequency map to track counts of elements
- int start = 0;
- int maxLength = 0;
- for (int end = 0; end < salesData.size(); ++end) {
- // Add current element to the frequency map
- freqMap[salesData[end]]++;
- // If any frequency exceeds the threshold, move `start` to reduce the window
- while (freqMap[salesData[end]] > frequencyThreshold) {
- freqMap[salesData[start]]--;
- if (freqMap[salesData[start]] == 0) {
- freqMap.erase(salesData[start]); // Remove element if its frequency is 0
- }
- start++;
- }
- // Update maxLength if the current window is longer
- maxLength = max(maxLength, end - start + 1);
- }
- return maxLength;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement