Final Array State After K Multiplication Operations II - Problem

You are given an integer array nums, an integer k, and an integer multiplier.

You need to perform k operations on nums. In each operation:

  • Find the minimum value x in nums.
  • If there are multiple occurrences of the minimum value, select the one that appears first.
  • Replace the selected minimum value x with x * multiplier.

After the k operations, apply modulo 10⁹ + 7 to every value in nums.

Return an integer array denoting the final state of nums after performing all k operations and then applying the modulo.

Input & Output

Example 1 — Basic Operations
$ Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
Output: [8,4,6,5,6]
💡 Note: Operation 1: min=1 at index 1, [2,2,3,5,6]. Operation 2: min=2 at index 0, [4,2,3,5,6]. Operation 3: min=2 at index 1, [4,4,3,5,6]. Operation 4: min=3 at index 2, [4,4,6,5,6]. Operation 5: min=4 at index 0, [8,4,6,5,6].
Example 2 — Small Array
$ Input: nums = [1,2], k = 3, multiplier = 4
Output: [16,8]
💡 Note: Operation 1: min=1 at index 0, [4,2]. Operation 2: min=2 at index 1, [4,8]. Operation 3: min=4 at index 0, [16,8].
Example 3 — Large Values with Modulo
$ Input: nums = [100000,2000], k = 2, multiplier = 1000000
Output: [999999307,999999993]
💡 Note: After operations array becomes very large, modulo 10^9+7 is applied: [100000000000, 2000000000] → [999999307, 999999993]

Constraints

  • 1 ≤ nums.length ≤ 104
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ 109
  • 1 ≤ multiplier ≤ 106

Visualization

Tap to expand
Final Array State After K Multiplication Operations II INPUT nums array: 2 1 3 5 6 indices: 0 1 2 3 4 Parameters: k = 5 multiplier = 2 Current minimum: 1 at index 1 Modulo: 10^9 + 7 (1000000007) ALGORITHM STEPS 1 Find Minimum Use min-heap for efficiency 2 Multiply x by 2 Replace min value in array 3 Repeat k times Perform 5 operations total 4 Apply Modulo Each value mod 10^9+7 Operation Trace: Op1: min=1 --> [2,2,3,5,6] Op2: min=2 --> [4,2,3,5,6] Op3: min=2 --> [4,4,3,5,6] Op4: min=3 --> [4,4,6,5,6] Op5: min=4 --> [8,4,6,5,6] All values < 10^9+7, no mod needed FINAL RESULT Output Array: 8 4 6 5 6 [8,4,6,5,6] OK - Verified Value Changes: 2 --> 4 --> 8 1 --> 2 --> 4 3 --> 6 5 (unchanged) Time: O(k log n) Space: O(n) Key Insight: Use a min-heap to efficiently find and update the minimum element in each operation. For large k values, optimize by detecting cycles when all elements reach similar magnitude, then use modular exponentiation to apply remaining multiplications efficiently. TutorialsPoint - Final Array State After K Multiplication Operations II | Optimal Solution
Asked in
Google 15 Amazon 12 Facebook 8
3.2K Views
Medium Frequency
~25 min Avg. Time
89 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen