Talentd/FleetCode/Problems/

3935. Power Update After K-th Largest Insertion I

Medium
Read SolutionWatch Video

3935. Power Update After K-th Largest Insertion I

Medium68.1% AcceptancePremium
PremiumFree on FleetCode

You are given an integer array nums and an integer p.

You are also given a 2D integer array queries, where each queries[i] = [vali, ki] and the difference between consecutive ki values is always less than 10.

For each query:

  • Insert vali into nums.
  • Let x be the kith largest element in the current nums.
  • Update p to px % (109 + 7).

Return an array ans where the ans[i] represents the value of p after processing the ith query.

Example 1:

Input: nums = [2], p = 4, queries = [[3,1],[1,2]]

Output: [64,4096]

Explanation:

i vali Current
nums
ki kith
largest
p New p = pk % (109 + 7)
0 3 [2, 3] 1 3 4 43 % (109 + 7) = 64
1 1 [2, 3, 1] 2 2 64 642 % (109 + 7) = 4096

Thus, ans = [64, 4096].

Example 2:

Input: nums = [7,5], p = 6, queries = [[4,3],[7,2]]

Output: [1296,220296870]

Explanation:

i vali Current​​​​​​​
nums
ki kith
largest
p New p = pk % (109 + 7)
0 4 [7, 5, 4] 3 4 6 64 % (109 + 7) = 1296
1 7 [7, 5, 4, 7] 2 7 1296 12967 % (109 + 7) = 220296870

Thus, ans = [1296, 220296870]

Constraints:

  • 1 <= nums.length <= 2 × 104
  • 1 <= nums[i] <= 106
  • ​​​​​​​1 <= p <= 106
  • 1 <= queries.length <= 2 × 104
  • ​​​​​​​1 <= vali <= 106
  • 1 <= ki <= n + i + 1
  • |ki - ki - 1| < 10 for i > 0
Asked by 1 company

Loading editor...

No test cases available.