DEV Community

Arpit Rathore
Arpit Rathore

Posted on

3264. Final Array State After K Multiplication Operations I

Important links

Intuition

  1. This is simple simulation problem.
  2. Write a method getMin() to find minimum value along with its index from the array (Create a record Pair to return 2 values).
  3. The idea is to iterate the array for k times.
  4. In each iteration, find the minimum value in the array along with its index using getMin().
  5. Update this value in the array using the index by setting nums[i] = minValue * multiplier

Solution

class Solution {

    record Pair(int value, int index) {}

    public int[] getFinalState(int[] nums, int k, int multiplier) {
        for (int i = 0; i < k; i++) {
            Pair minPair = getMin(nums);

            nums[minPair.index] = minPair.value * multiplier;
            System.out.printf("Min %s. nums after replacement: %s %n", minPair, Arrays.toString(nums));
        }
        return nums;
    }

    private Pair getMin(int[] nums) {
        Pair minPair = new Pair(Integer.MAX_VALUE, -1);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < minPair.value) {
                minPair = new Pair(nums[i], i);
            }
        }
        return minPair;
    }
}
Enter fullscreen mode Exit fullscreen mode

Visualisation

I have added print statement to visualise the minValue and nums in each iteration:

Input: nums=[2,1,3,5,6], k=5, multiplier=2

Std out:
Min Pair[value=1, index=1]. nums after replacement: [2, 2, 3, 5, 6] 
Min Pair[value=2, index=0]. nums after replacement: [4, 2, 3, 5, 6] 
Min Pair[value=2, index=1]. nums after replacement: [4, 4, 3, 5, 6] 
Min Pair[value=3, index=2]. nums after replacement: [4, 4, 6, 5, 6] 
Min Pair[value=4, index=0]. nums after replacement: [8, 4, 6, 5, 6]  
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay