FastPrepPower Update After K-th Largest Insertion

Power Update After K-th Largest Insertion

Google logoGoogle● HardINTERNOA
Learn

Problem statement

You are given an integer array nums, an initial integer p, and a list of queries. Each query is a pair [value, k].

Process the queries in order. For each query:

  1. Insert value into the multiset of stored numbers.
  2. Let x be the k-th largest stored number. Equal values occupy separate ranks.
  3. Update p to p^x mod (10^9 + 7).
  4. Append the updated p to the answer.

Return the answer array after every query has been processed.

Function

powerAfterInsertions(nums: int[], p: int, queries: int[][]) → int[]

Examples

Example 1

nums = [5,1,3]p = 2queries = [[4,2],[10,1]]return = [16,511620083]

After inserting 4, the second-largest value is 4, so p = 2^4 = 16. After inserting 10, the largest value is 10, so p = 16^10 mod (10^9 + 7) = 511620083.

Example 2

nums = [2]p = 3queries = [[2,2],[1,3]]return = [9,9]

Duplicates take separate ranks. After the first insertion, the second-largest value is 2 and p becomes 9. After inserting 1, the third-largest value is 1, so p remains 9.

Constraints

  • 1 <= nums.length <= 10^5.
  • 1 <= queries.length <= 10^5.
  • 1 <= nums[i] <= 10^9.
  • Each query is [value, k] with 1 <= value <= 10^9.
  • For every query, 1 <= k and k does not exceed the number of stored values after the insertion.
  • 0 <= p < 10^9 + 7.

More Google problems

See Google hiring insights
public int[] powerAfterInsertions(int[] nums, int p, int[][] queries) {
    // Write your code here.
}
nums[5,1,3]
p2
queries[[4,2],[10,1]]
expected[16,511620083]
Checking account…