Leetcode Problem 2541. Minimum Operations to Make Array Equal II

2541. Minimum Operations to Make Array Equal II

Leetcode Solutions

Balancing Increments and Decrements

  1. Initialize two variables, increments and decrements, to keep track of the total number of increments and decrements needed.
  2. Iterate over the arrays nums1 and nums2.
    • For each index i, calculate the difference diff between nums1[i] and nums2[i].
    • If diff is positive, add diff / k to decrements (after checking if diff is divisible by k).
    • If diff is negative, add the absolute value of diff / k to increments (after checking if diff is divisible by k).
    • If diff is not divisible by k, return -1.
  3. After the loop, if increments equals decrements, return increments.
  4. If increments does not equal decrements, return -1.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...