Problem Β· Sorting
Get Success Value π (Fungible :)
Learn this problemProblem statement
Amazon Prime Video has recently released an exclusive series on its platform. They collected the number of viewers from n
different regions across the world and stored the data in the array num_viewers.
The success value of the release is defined as the sum of viewership in the top k regions, those with the highest viewers.
For example, if num_viewers = [3, 2, 1, 4, 5] and k = 3, then the success value of the release is
3 + 4 + 5 = 12 as [3, 4, 5] are the top 3 values.
Given a number of k values, calculate the success value for each query.
Function
findSuccessValue(num_viewers: int[], queries: int[]) β long[]Complete the function findSuccessValue in the editor.
findSuccessValue has the following parameters:
int num_viewers[n]: viewership from n different regionsint queries[q]: an array of k valuesReturns
long[q]: the maximum possible success values for each queryExamples
Example 1
num_viewers = [2, 5, 6, 3, 5]queries = [2, 3, 5]return = [11, 16, 21]The viewership in 5 regions is num_viewers = [2, 5, 6, 3, 5], and we want to find the success value for 3 queries that are queries = [2, 3, 5].
For the first query, k = 2, the viewership of the top 2 regions is [6, 5]. The success value is 6 + 5 = 11.
For the second query, k = 3, the viewership of the top 3 regions is [6, 5, 5] and 6 + 5 + 5 = 16.
For the third query, k = 5, all the 5 regions are used for the success value and 6 + 5 + 5 + 3 + 2 = 21.
Return [11, 16, 21].
Example 2
num_viewers = [7, 3, 5, 2]queries = [1, 4]return = [7, 17]For the first query, k = 1, only the top region is used for the success value.
For the second query, k = 4, all 4 regions are used.
Return [7, 17].
Example 3
num_viewers = [7, 5, 6]queries = [1, 2, 3]return = [7, 13, 18]For the first query, k = 1, only the top region is used for the success value.
For the second query, k = 2, only the top 2 regions are used.
For the third query, k = 3, all 3 regions are used.
Return [7, 13, 18].
Constraints
1 <= n <= 1051 <= q <= 1051 <= num_viewers[i] <= 1091 <= queries[i] <= n1014 and requires a 64-bit integer.