Problem solution · Java

Minimum Operations to Make Subarray Elements Equal

Minimum Operations to Make Subarray Elements Equal: a Java 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
86 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 Minimum Operations to Make Subarray Elements Equal, 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

  • 86 lines of Java from the credited upstream file 3422.java.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 4 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 codeMinimum Operations to Make Subarray Elements Equal · JavaJava
Use this to learn the idea, then write your own version.
class SumMultiset {  public TreeMap<Integer, Integer> nums = new TreeMap<>();  public long sum = 0;  public int size = 0;   public void insert(int val) {    nums.merge(val, 1, Integer::sum);    sum += val;    ++size;  }   public void erase(int val) {    nums.merge(val, -1, Integer::sum);    if (nums.get(val) == 0)      nums.remove(val);    sum -= val;    --size;  }} class MedianTracker {  public MedianTracker(int k) {    this.k = k;  }   public void add(int val) {    below.insert(val);    balance();  }   public void remove(int val) {    if (below.nums.containsKey(val))      below.erase(val);    else      above.erase(val);  }   public long getCost() {    return above.sum - below.sum - (k % 2 == 1 ? above.nums.firstKey() : 0L);  }   private SumMultiset below = new SumMultiset();  private SumMultiset above = new SumMultiset();  private int k;   private void balance() {    // Move excessive numbers from `below` to `above`.    while (below.size > k / 2) {      final int mx = below.nums.lastKey();      below.erase(mx);      above.insert(mx);    }     // Balance `below` and `above`.    while (!above.nums.isEmpty()) {      final int mx = below.nums.lastKey();      final int mn = above.nums.firstKey();      if (mx <= mn)        break;      below.erase(mx);      above.erase(mn);      below.insert(mn);      above.insert(mx);    }  }} class Solution {  public long minOperations(int[] nums, int k) {    MedianTracker tracker = new MedianTracker(k);     for (int i = 0; i < k; ++i)      tracker.add(nums[i]);     long ans = tracker.getCost();     for (int i = k; i < nums.length; ++i) {      tracker.remove(nums[i - k]);      tracker.add(nums[i]);      ans = Math.min(ans, tracker.getCost());    }     return ans;  }} 

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 ↗