Problem solution · C++

Final Array State After K Multiplication Operations II

Final Array State After K Multiplication Operations II: a C++ solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
63 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Final Array State After K Multiplication Operations II, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 63 lines of C++ from the credited upstream file 3266.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 6 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFinal Array State After K Multiplication Operations II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {    if (multiplier == 1)      return nums;     const int n = nums.size();    const int maxNum = ranges::max(nums);    vector<int> ans(n);    using P = pair<int, int>;  // (nums[i], i)    priority_queue<P, vector<P>, greater<>> minHeap;     for (int i = 0; i < n; ++i)      minHeap.emplace(nums[i], i);     // Keep multiplying the minimum number as close as possible to the maximum    // number in the array. After that, stop multiplying the minimum number    // because it will be greater than the maximum number in the array and break    // the circularity.    while (k > 0 &&           static_cast<long>(minHeap.top().first) * multiplier <= maxNum) {      const auto [num, i] = minHeap.top();      minHeap.pop();      minHeap.emplace(num * multiplier, i);      --k;    }     vector<pair<int, int>> sortedIndexedNums;    while (!minHeap.empty())      sortedIndexedNums.push_back(minHeap.top()), minHeap.pop();     const int multipliesPerNum = k / n;    const int remainingK = k % n;     // Evenly distribute the remaining multiplications to each number by using    // fast exponentiation.    for (auto& [num, _] : sortedIndexedNums)      num = (num * modPow(multiplier, multipliesPerNum)) % kMod;     // Distribute the remaining multiplications to the minimum `remainingK`    // numbers.    for (int i = 0; i < remainingK; ++i)      sortedIndexedNums[i].first =          (static_cast<long>(sortedIndexedNums[i].first) * multiplier % kMod);     for (const auto& [num, i] : sortedIndexedNums)      ans[i] = num;     return ans;  }  private:  static constexpr int kMod = 1'000'000'007;   long modPow(long x, long n) {    if (n == 0)      return 1;    if (n % 2 == 1)      return x * modPow(x, n - 1) % kMod;    return modPow(x * x % kMod, n / 2);  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗