We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Final Array State After K Multiplication Operations I

Number: 3555

Difficulty: Easy

Paid? No

Companies: Bloomberg


Problem Description

Given an integer array nums, an integer k, and an integer multiplier, perform k operations on nums. In each operation, find the minimum value in nums (if there are duplicates, choose the first occurrence) and replace it with x * multiplier. Return the final state of the array after all k operations.


Key Insights

  • The array is small in size (nums.length up to 100, k up to 10), so a simple linear scan is efficient.
  • For each operation, we look for the first occurrence of the minimum element.
  • Direct simulation of the operations is enough; no advanced data structure (like a heap) is required despite its tag because the constraints are low.
  • Multiplying the selected element might change the order in subsequent operations.

Space and Time Complexity

Time Complexity: O(k * n), where n is the length of nums.
Space Complexity: O(1) additional space (in-place modification of the array).


Solution

The solution simulates exactly k operations. For each operation, traverse the array to find the index of the minimum element (respecting the first occurrence in case of ties). Then, update the element by multiplying it with the multiplier. This approach is straightforward due to the small size of the input arrays as described in the constraints.


Code Solutions

# Function to perform k operations on the nums array
def finalArrayState(nums, k, multiplier):
    # Perform k operations
    for _ in range(k):
        # Initialize index and value for tracking the minimum
        min_index = 0
        min_value = nums[0]
        # Find the first occurrence of the minimum value in the list
        for index in range(len(nums)):
            if nums[index] < min_value:
                min_value = nums[index]
                min_index = index
        # Multiply the found minimum element by the multiplier
        nums[min_index] = nums[min_index] * multiplier
    # Return the modified array after k operations
    return nums

# Example usage:
# nums = [2, 1, 3, 5, 6], k = 5, multiplier = 2 should output [8, 4, 6, 5, 6]
print(finalArrayState([2, 1, 3, 5, 6], 5, 2))
← Back to All Questions