Problem solution · C++

Minimum Operations to Make Array Modulo Alternating I

Minimum Operations to Make Array Modulo Alternating I: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Sliding window or two pointers
Source
Kamyu LeetCode Solutions
Length
52 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

Sliding window or two pointers

For Minimum Operations to Make Array Modulo Alternating I, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 52 lines of C++ from the credited upstream file minimum-operations-to-make-array-modulo-alternating-i.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

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

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Operations to Make Array Modulo Alternating I · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n + k)// Space: O(k) // freq table, sliding windowclass Solution {public:    long long minOperations(vector<int>& nums, int k) {        const auto& topk = [](const vector<int64_t>& nums, int k) {  // Time: O(k * n)            vector<pair<int64_t, int>> result(k, pair(numeric_limits<int64_t>::max(), numeric_limits<int>::max()));            for (int i = 0; i < size(nums); ++i) {                auto x = pair(nums[i], i);                for (auto& y : result) {                    if (x < y) {                        swap(x, y);                    }                }            }            return result;        };         const auto& distance = [&](const auto& cnt) {            const auto& total = accumulate(cbegin(cnt), cend(cnt), 0);            int c = accumulate(cbegin(cnt) + 1, cbegin(cnt) + (k / 2 + 1), 0);            vector<int64_t> dist(k);            for (int i = 0; i < size(cnt); ++i) {                dist[0] += cnt[i] * min(i, k - i);            }            for (int i = 1; i < size(dist); ++i) {                dist[i] = dist[i - 1] - c + (total - c) - (k % 2 ? cnt[(i + k / 2) % k] : 0);                c += cnt[(i + k / 2) % k] - cnt[i];            }            return dist;        };         vector<vector<int>> cnt(2, vector<int>(k));        for (int i = 0; i < size(nums); ++i) {            ++cnt[i % 2][nums[i] % k];        }        vector<vector<int64_t>> dist(2);        for (int i = 0; i < 2; ++i) {            dist[i] = distance(cnt[i]);        }        vector<vector<pair<int64_t, int>>> top2(2);        for (int i = 0; i < 2; ++i) {            top2[i] = topk(dist[i], 2);        }        return top2[0][0].second == top2[1][0].second ? min(top2[0][0].first + top2[1][1].first, top2[0][1].first + top2[1][0].first) : top2[0][0].first + top2[1][0].first;     }}; 

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 ↗