Posts

3266. Final Array State After K Multiplication Operations II - Solved Code along with explanation

Image
3266. Final Array State After K Multiplication Operations II In this Problem we will be using a priority queue and a trick, to reduce the complexity.  The task is simply to return an array (vector) that contains the final state of all the Numbers after performing all k Number of operations and then applying the Modulo. First, we are passing the 'nums' vector by reference, so we are not creating a copy of the 'nums' vector but we are actually using the 'nums' vector, and making the desired changes, which is faster.  that's why the use of '&'. We do the operation using a simple for loop but then we do the operation Let's say we have k = 11, and multiplier = 2 when k = 1, [5,2,6,5,6] when k = 2, [5,4,6,5,6] when k = 3, [5,8,6,5,6] when k = 4, [10,8,6,5,6] when k = 5, [10,8,6,10,6] when k = 6, [10,8,12,10,6] when k = 7, [10,8,12,10,12] - State achieved!  initially we have a smallest number '2' in the nums array [5,2,6,5,6], and we keep o...