-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalArrayAfterKMultiple.java
More file actions
44 lines (33 loc) · 956 Bytes
/
Copy pathFinalArrayAfterKMultiple.java
File metadata and controls
44 lines (33 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Problem: Final Array State After K Multiplication Operations
Description:
Perform k operations on the array.
In each operation:
1. Find the minimum element.
2. If multiple minimum elements exist, choose the first one.
3. Replace it with element * multiplier.
Return the final array.
Approach:
- Repeat k times.
- Find the index of the minimum element.
- Multiply it by multiplier.
Time Complexity: O(k * n)
Space Complexity: O(1)
*/
class Solution {
public int[] getFinalState(int[] nums, int k, int multiplier) {
// Perform k operations
while (k-- > 0) {
int minIndex = 0;
// Find index of minimum element
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[minIndex]) {
minIndex = i;
}
}
// Multiply the minimum element
nums[minIndex] *= multiplier;
}
return nums;
}
}