559. Maximum Frequency Score of a Subarray

A positive integer k and an integer array nums are provided to you.

An array's frequency score is calculated by taking the sum modulo 109 + 7 and raising each of the array's unique values to the power of their frequencies.

  • For instance, the array [5,4,5,7,4,4] has a frequency score of (43 + 52 + 71) modulo (109 + 7) = 96.

Give back a subarray of size k's maximum frequency score in nums. Instead of maximising the real value, you should maximise the value under the modulo.

A continuous portion of an array is called a subarray.

Example 1:

Input : nums = [2, 5, 2, 5, 3, 2] , k = 3

Output : 27

Explanation :

The subarray [5, 2, 5] has a frequency score equal to 27. It can be shown that it is the maximum frequency score we can have.

Example 2:

Input : nums = [1, 2, 3, 3, 2, 10] , k = 4

Output : 21

Explanation :

The subarray [3, 3, 2, 10] has a frequency score equal to 21. It can be shown that it is the maximum frequency score we can have.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [6, 9, 1, 4, 1, 9, 6, 6, 4] , k = 6

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxFrequencyScore(vector<int>& nums, int k) {
//your code goes here
}
};
Test Case

Input:

K
Nums