Difficulty: Easy
Topics: Arrays
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:
x
in nums
. If there are multiple occurrences of the minimum value, select the one that appears first.x
with x * multiplier
.Return an integer array denoting the final state of nums
after performing all k
operations.
Input:
nums = [2,1,3,5,6], k = 5, multiplier = 2
Output:
[8,4,6,5,6]
Explanation:
Operation | Result |
---|---|
After 1 | [2, 2, 3, 5, 6] |
After 2 | [4, 2, 3, 5, 6] |
After 3 | [4, 4, 3, 5, 6] |
After 4 | [4, 4, 6, 5, 6] |
After 5 | [8, 4, 6, 5, 6] |
Input:
nums = [1,2], k = 3, multiplier = 4
Output:
[16,8]
Explanation:
Operation | Result |
---|---|
After 1 | [4, 2] |
After 2 | [4, 8] |
After 3 | [16, 8] |
Loading component...
Loading component...
Main Function is not defined.
INPUT: nums = [2,1,3,5,6], k = 5, multiplier = 2
OUTPUT: [8,4,6,5,6]
public static int[] getFinalState(int[] nums , int k, int multiplier) {
int[] arr = new int[2];
for (int i = 0; i < k; i++) {
arr = getMin(nums);
nums[ arr[1] ] = arr[0] * multiplier;
}
return nums;
}//function end
private static int[] getMin( int[] nums) {
int min = Integer.MAX_VALUE;
int idx = 0;
for(int i =0; i < nums.length; i++){
if(nums[i] < min){
min = nums[1];
idx = i;
}
}
return new int[] { min , idx };
} //Function End