Final Array State After K Multiplication Operations I
EASYDescription
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
xinnums. If there are multiple occurrences of the minimum value, select the one that appears first. - Replace the selected minimum value
xwithx * multiplier.
Return an integer array denoting the final state of nums after performing all k operations.
Example 1:
Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
Output: [8,4,6,5,6]
Explanation:
| Operation | Result |
|---|---|
| After operation 1 | [2, 2, 3, 5, 6] |
| After operation 2 | [4, 2, 3, 5, 6] |
| After operation 3 | [4, 4, 3, 5, 6] |
| After operation 4 | [4, 4, 6, 5, 6] |
| After operation 5 | [8, 4, 6, 5, 6] |
Example 2:
Input: nums = [1,2], k = 3, multiplier = 4
Output: [16,8]
Explanation:
| Operation | Result |
|---|---|
| After operation 1 | [4, 2] |
| After operation 2 | [4, 8] |
| After operation 3 | [16, 8] |
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1001 <= k <= 101 <= multiplier <= 5
Approaches
Checkout 2 different approaches to solve Final Array State After K Multiplication Operations I. Click on different approaches to view the approach and algorithm in detail.
Brute-Force Simulation
This approach directly simulates the process described in the problem. It iterates k times, and in each iteration, it scans the entire array to find the first occurrence of the minimum value and then updates it.
Algorithm
-
- Repeat the following process
ktimes.
- Repeat the following process
-
- Initialize
minIndexto -1 andminValueto a very large number (e.g.,Integer.MAX_VALUE).
- Initialize
-
- Iterate through the
numsarray from left to right (indexjfrom 0 ton-1).
- Iterate through the
-
- If the current element
nums[j]is less thanminValue, updateminValuetonums[j]andminIndextoj.
- If the current element
-
- After the inner loop completes,
minIndexwill hold the index of the first occurrence of the minimum value.
- After the inner loop completes,
-
- Update the array at the found index:
nums[minIndex] = nums[minIndex] * multiplier.
- Update the array at the found index:
-
- After the outer loop of
kiterations finishes, return the modifiednumsarray.
- After the outer loop of
This method directly translates the problem statement into code. We loop k times to perform the k operations. In each operation, we iterate through the entire nums array to find the minimum value and its first index. The tie-breaking rule (selecting the one that appears first) is naturally handled by iterating from the beginning of the array and only updating the minimum index when a strictly smaller value is found. Once the target element is identified, we update its value by multiplying it with the multiplier. This process is repeated k times on the same array.
class Solution {
public int[] getFinalArray(int[] nums, int k, int multiplier) {
int n = nums.length;
for (int i = 0; i < k; i++) {
int minIndex = -1;
int minValue = Integer.MAX_VALUE;
// Find the first occurrence of the minimum value
for (int j = 0; j < n; j++) {
if (nums[j] < minValue) {
minValue = nums[j];
minIndex = j;
}
}
// Update the value at the found index
if (minIndex != -1) {
nums[minIndex] = nums[minIndex] * multiplier;
}
}
return nums;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Low memory overhead as it modifies the array in-place.
- Inefficient for large inputs, as it requires scanning the entire array in each of the
koperations.
Code Solutions
Checking out 3 solutions in different languages for Final Array State After K Multiplication Operations I. Click on different languages to view the code.
class Solution {
public
int[] getFinalState(int[] nums, int k, int multiplier) {
PriorityQueue<Integer> pq = new PriorityQueue<>(
(i, j)->nums[i] - nums[j] == 0 ? i - j : nums[i] - nums[j]);
for (int i = 0; i < nums.length; i++) {
pq.offer(i);
}
while (k-- > 0) {
int i = pq.poll();
nums[i] *= multiplier;
pq.offer(i);
}
return nums;
}
}
Video Solution
Watch the video walkthrough for Final Array State After K Multiplication Operations I
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.