Talentd
FleetCode
Make and follow sheets.Sign in

Final Array State After K Multiplication Operations I solution and explanation

EasyArrayHeap (Priority Queue)MathSimulation6 min read86.9% acceptanceUpdated 9 Apr 2026
Final Array State After K Multiplication Operations I | Detailed | Leetcode 3264 | codestorywithMIKcodestorywithMIK · 23:19 · 4K viewsWatch on YouTube
9 more videos on this problem
  1. Final Array State After K Multiplication Operations I | Detailed | Leetcode 3264 | codestorywithMIKcodestorywithMIK · 23:19 · 4K views
  2. Final Array State After K Multiplication Operations I - Leetcode 3264 - PythonNeetCodeIO · 7:49 · 3K views
  3. Final Array State After K Multiplication Operations I | Leetcode 3264Techdose · 7:34 · 1K views
  4. 3264 Final Array State After K Multiplication Operations I || O(N*N) to O(K*logN) 🔥|| Priority_queueAyush Rao · 8:01 · 512 views
  5. 3264. Final Array State After K Multiplication Operations I (Leetcode Easy)Programming Live with Larry · 8:02 · 424 views
  6. LeetCode 3264 Final Array State After K Multiplication Operations I | Heap | Asked in AmazonCode Harmony · 13:14 · 217 views
  7. Leetcode POTD 3264. Final Array State After K Multiplication Operations I || DSA in JavascriptThe Logical Loop · 13:09 · 97 views
  8. Leetcode | 3264. Final Array State After K Multiplication Operations ITofayel Ahmmed · 24:19 · 71 views
  9. 中文解释Leetcode: 3264. Final Array State After K Multiplication Operations ITimothy Lee Grant · 32:29 · 60 views
  10. 3264 Final Array State After K Multiplication Operations ICodeCracker · 3:53 · 56 views

The 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.

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 <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 10
  • 1 <= multiplier <= 5

Approach overview

Problem Overview: You receive an integer array nums, an integer k, and a multiplier. Perform exactly k operations. In each operation, find the smallest value in the array (choose the smallest index if there is a tie), multiply it by multiplier, and update the array. After all operations, return the final state of the array.

This is mainly a simulation problem built around repeatedly selecting the minimum element. The main design decision is how efficiently you retrieve that minimum value during each operation.

Approach 1: Simple Iteration Approach (O(n * k) time, O(1) space)

The most straightforward solution simulates the process exactly as described. For each of the k operations, iterate through the entire array to find the smallest value and its index. Once found, multiply that element by multiplier and continue to the next operation. The algorithm uses a simple linear scan of the array each time the minimum must be identified.

This approach works well when k and n are small. The implementation is extremely simple and mirrors the problem statement directly. However, every operation requires scanning the entire array, resulting in O(n) work per operation and O(n * k) total time.

Approach 2: Using Min-Heap (Priority Queue) (O((n + k) log n) time, O(n) space)

A more efficient solution maintains the current minimum using a min-heap (priority queue). Push all elements into the heap as pairs (value, index). The heap automatically keeps the smallest value at the top, and the index ensures ties are resolved correctly.

During each operation, pop the smallest element from the heap, multiply its value by multiplier, update the corresponding index in the array, and push the updated pair back into the heap. Each pop and push operation costs O(log n), so performing k operations results in O(k log n) heap work after the initial O(n) heap construction.

This method avoids repeated full scans of the array and is ideal when k is large. It models the process as a simulation but relies on a heap to keep minimum lookups efficient.

Recommended for interviews: Start by explaining the direct simulation using iteration to demonstrate understanding of the rules. Then move to the min‑heap optimization. Interviewers typically expect the heap solution because it reduces repeated minimum searches and scales better when the number of operations grows.

Approach 1: Simple Iteration Approach

This approach involves simply iterating through the array to find the minimum element and multiplying it by the multiplier. This process is repeated for k times. It's straightforward due to the relatively small constraints (nums.length <= 100).

def final_array_state(nums, k, multiplier):
    for _ in range(k):
        min_index = min(range(len(nums)), key=nums.__getitem__)
        nums[min_index] *= multiplier
    return nums

nums = [2, 1, 3, 5, 6]
k = 5
multiplier = 2
print(final_array_state(nums, k, multiplier))
import java.util.List;
import java.util.ArrayList;

public class Solution {
    public static void finalArrayState(List<Integer> nums, int k, int multiplier) {
        int numsSize = nums.size();
        for (int i = 0; i < k; i++) {
            int minIndex = 0;
            for (int j = 1; j < numsSize; j++) {
                if (nums.get(j) < nums.get(minIndex)) {
                    minIndex = j;
                }
            }
            nums.set(minIndex, nums.get(minIndex) * multiplier);
        }
    }
    
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(2);
        nums.add(1);
        nums.add(3);
        nums.add(5);
        nums.add(6);
        
        int k = 5;
        int multiplier = 2;
        
        finalArrayState(nums, k, multiplier);

        System.out.println(nums);
    }
}
#include <iostream>
#include <vector>

void finalArrayState(std::vector<int>& nums, int k, int multiplier) {
    int numsSize = nums.size();
    while (k--) {
        int minIndex = 0;
        for (int i = 1; i < numsSize; ++i) {
            if (nums[i] < nums[minIndex]) {
                minIndex = i;
            }
        }
        nums[minIndex] *= multiplier;
    }
}

int main() {
    std::vector<int> nums = {2, 1, 3, 5, 6};
    int k = 5, multiplier = 2;

    finalArrayState(nums, k, multiplier);

    for (int num : nums) {
        std::cout << num << " ";
    }
    return 0;
}
function finalArrayState(nums, k, multiplier) {
    for (let i = 0; i < k; i++) {
        let minIndex = 0;
        for (let j = 1; j < nums.length; j++) {
            if (nums[j] < nums[minIndex]) minIndex = j;
        }
        nums[minIndex] *= multiplier;
    }
    return nums;
}

let nums = [2, 1, 3, 5, 6];
let k = 5;
let multiplier = 2;
console.log(finalArrayState(nums, k, multiplier));
#include <stdio.h>

void finalArrayState(int* nums, int numsSize, int k, int multiplier) {
    for (int i = 0; i < k; ++i) {
        int minIndex = 0;
        for (int j = 1; j < numsSize; ++j) {
            if (nums[j] < nums[minIndex]) {
                minIndex = j;
            }
        }
        nums[minIndex] *= multiplier;
    }
}

int main() {
    int nums[] = {2, 1, 3, 5, 6};
    int k = 5, multiplier = 2;
    int size = sizeof(nums)/sizeof(nums[0]);

    finalArrayState(nums, size, k, multiplier);

    // Output the results
    for(int i = 0; i < size; i++) {
        printf("%d ", nums[i]);
    }
    return 0;
}
using System;
using System.Linq;

class Program
{
    static void FinalArrayState(int[] nums, int k, int multiplier)
    {
        for (int i = 0; i < k; ++i)
        {
            int minIndex = Array.IndexOf(nums, nums.Min());
            nums[minIndex] *= multiplier;
        }
    }

    static void Main()
    {
        int[] nums = {2, 1, 3, 5, 6};
        int k = 5, multiplier = 2;
        FinalArrayState(nums, k, multiplier);

        Console.WriteLine(string.Join(" ", nums));
    }
}

This function calculates the index of the minimum value in the array nums using the min function with a key parameter. It then multiplies this minimum value by multiplier exactly k times.

Complexity

Time Complexity: O(k * n), as finding the minimum value for k times requires scanning the list.
Space Complexity: O(1), since the solution modifies the list in place.

Try this approach in the editor

Approach 2: Using Min-Heap (Priority Queue)

In this approach, a min-heap (priority queue) is used to efficiently retrieve the minimum element. The heap is updated on every multiplication operation, ensuring that the minimum value is always at the root.

import heapq

def final_array_state(nums, k, multiplier):
    heap = [(val, i) for i, val in enumerate(nums)]
    heapq.heapify(heap)
    nums_indices = {i: val for i, val in enumerate(nums)}

    while k > 0:
        # Get the current minimum
        min_val, min_idx = heapq.heappop(heap)

        # Release the new minimum after update
        if nums_indices[min_idx] == min_val:  # Ensure it's the latest value
            nums[min_idx] *= multiplier
            new_val = nums[min_idx]

            # Push the new value with the same index back to the heap
            heapq.heappush(heap, (new_val, min_idx))
            nums_indices[min_idx] = new_val
            k -= 1

    return nums

nums = [2, 1, 3, 5, 6]
k = 5
multiplier = 2
print(final_array_state(nums, k, multiplier))
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

class ArrayElement {
    int value;
    int index;

    ArrayElement(int value, int index) {
        this.value = value;
        this.index = index;
    }
}

public class Solution {
    public static List<Integer> finalArrayState(List<Integer> nums, int k, int multiplier) {
        PriorityQueue<ArrayElement> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a.value));
        Map<Integer, Integer> indexValueMap = new HashMap<>();

        for (int i = 0; i < nums.size(); i++) {
            heap.offer(new ArrayElement(nums.get(i), i));
            indexValueMap.put(i, nums.get(i));
        }

        while (k > 0 && !heap.isEmpty()) {
            ArrayElement minElem = heap.poll();
            if (indexValueMap.get(minElem.index) == minElem.value) {
                int newValue = minElem.value * multiplier;
                nums.set(minElem.index, newValue);
                indexValueMap.put(minElem.index, newValue);
                heap.offer(new ArrayElement(newValue, minElem.index));
                k--;
            }
        }

        return nums;
    }

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(2);
        nums.add(1);
        nums.add(3);
        nums.add(5);
        nums.add(6);

        int k = 5;
        int multiplier = 2;

        List<Integer> finalState = finalArrayState(nums, k, multiplier);

        System.out.println(finalState);
    }
}

The function uses a min-heap, built with tuples containing the element and its index. This allows the heap to manage duplicate values correctly and to quickly access and update the minimum value. It ensures the constraint of selecting the first occurrence of a minimum value by utilizing the index in conjunction with the heap operations.

Complexity

Time Complexity: O(n + k log n), where n is the size of the array for the heapify operation, and k log n for the subsequent heap operations on each element.
Space Complexity: O(n) because a heap stores all elements and additional structure is used for managing index tracking.

Try this approach in the editor

Approach 3: Priority Queue (Min-Heap) + Simulation

We can use a min-heap to maintain the elements in the array nums. Each time, we extract the minimum value from the min-heap, multiply it by multiplier, and then put it back into the min-heap. During the implementation, we insert the indices of the elements into the min-heap and define a custom comparator function to sort the min-heap based on the values of the elements in nums as the primary key and the indices as the secondary key.

Finally, we return the array nums.

The time complexity is O((n + k) times log n), and the space complexity is O(n). Here, n is the length of the array nums.

function getFinalState(nums: number[], k: number, multiplier: number): number[] {
    const pq = new PriorityQueue<number>((i, j) =>
        nums[i] === nums[j] ? i - j : nums[i] - nums[j],
    );

    for (let i = 0; i < nums.length; ++i) {
        pq.enqueue(i);
    }
    while (k--) {
        const i = pq.dequeue()!;
        nums[i] *= multiplier;
        pq.enqueue(i);
    }
    return nums;
}
func getFinalState(nums []int, k int, multiplier int) []int {
	h := &hp{nums: nums}
	for i := range nums {
		heap.Push(h, i)
	}

	for k > 0 {
		i := heap.Pop(h).(int)
		nums[i] *= multiplier
		heap.Push(h, i)
		k--
	}

	return nums
}

type hp struct {
	sort.IntSlice
	nums []int
}

func (h *hp) Less(i, j int) bool {
	if h.nums[h.IntSlice[i]] == h.nums[h.IntSlice[j]] {
		return h.IntSlice[i] < h.IntSlice[j]
	}
	return h.nums[h.IntSlice[i]] < h.nums[h.IntSlice[j]]
}

func (h *hp) Pop() any {
	old := h.IntSlice
	n := len(old)
	x := old[n-1]
	h.IntSlice = old[:n-1]
	return x
}

func (h *hp) Push(x any) {
	h.IntSlice = append(h.IntSlice, x.(int))
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Simple Iteration Approach

Time Complexity: O(k * n), as finding the minimum value for k times requires scanning the list.
Space Complexity: O(1), since the solution modifies the list in place.

Using Min-Heap (Priority Queue)

Time Complexity: O(n + k log n), where n is the size of the array for the heapify operation, and k log n for the subsequent heap operations on each element.
Space Complexity: O(n) because a heap stores all elements and additional structure is used for managing index tracking.

Priority Queue (Min-Heap) + Simulation–

Complexity analysis

ApproachTimeSpaceWhen to Use
Simple Iteration (Linear Scan)O(n * k)O(1)Small arrays or small k where simplicity matters more than optimization
Min-Heap (Priority Queue)O((n + k) log n)O(n)Large k or repeated minimum lookups where heap structure avoids full scans

Frequently asked questions

Is Final Array State After K Multiplication Operations I easy or hard?

The problem is classified as Easy with a high acceptance rate around 86%. The core idea is simple simulation, while the heap optimization introduces a common interview pattern involving priority queues.

Final Array State After K Multiplication Operations I Python/Java solution

Python solutions typically use the built-in heapq module to implement the min-heap, while Java uses PriorityQueue. Both implementations store (value, index) pairs so the smallest value and its index can be processed correctly during each operation.

How to solve Final Array State After K Multiplication Operations I in O(n)?

An exact O(n) solution is not typical for this problem because each operation may change which element becomes the next minimum. The closest practical optimization uses a min-heap, which reduces repeated minimum searches and runs in O((n + k) log n) time.

What is the best approach for Final Array State After K Multiplication Operations I?

The most efficient approach uses a min-heap (priority queue). Store pairs of (value, index) so the smallest value can be retrieved in O(log n) time. Each of the k operations pops the minimum, multiplies it, and pushes it back. This results in O((n + k) log n) time and O(n) space.

Is Final Array State After K Multiplication Operations I asked at Google/Amazon/Meta?

Problems involving heaps, repeated minimum selection, and simulation patterns appear frequently in interviews at companies like Amazon and Google. Variations of this question test your ability to optimize repeated operations using a priority queue.

What data structure is used in Final Array State After K Multiplication Operations I?

The optimized solution uses a min-heap (priority queue). This structure efficiently tracks the smallest element and supports fast insertion and removal in O(log n) time, which is crucial when performing k repeated operations.

What is the time complexity of Final Array State After K Multiplication Operations I?

The simple simulation approach takes O(n * k) time because the algorithm scans the entire array during each operation to find the minimum. The optimized min-heap approach reduces this to O((n + k) log n) by maintaining the smallest element in a priority queue.

Comments

Now write it yourselfClose this page and solve Final Array State After K Multiplication Operations I in the editor, without looking.
Practise Final Array State After K Multiplication Operations I