LeetCode 2934: Minimum Operations to Maximize Last Elements in Arrays Solution

Master LeetCode problem 2934 (Minimum Operations to Maximize Last Elements in Arrays), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2934. Minimum Operations to Maximize Last Elements in Arrays

Problem Explanation

Explanation

To solve this problem, we need to find the minimum number of operations required to make the last element in both arrays equal to the maximum element in each array. We can achieve this by iterating through the arrays and checking if the current maximum elements are already at the last position. If not, we can try to swap elements to move the maximum elements to the last position.

Here is the algorithm:

  1. Initialize two variables max1 and max2 to store the maximum elements in nums1 and nums2, respectively.
  2. Iterate through both arrays:
    • If the current element is greater than max1, update max1 and store the index.
    • If the current element is greater than max2, update max2 and store the index.
  3. If max1 and max2 are already at the last position, return 0 (no operations needed).
  4. If one of max1 or max2 is at the last position, swap the elements at the stored index to move the maximum element to the last position.
  5. If both max1 and max2 are not at the last position, check if swapping the elements at the stored indices will result in both conditions being satisfied.
  6. Return the minimum number of operations needed or -1 if it is impossible to satisfy both conditions.

The time complexity of this algorithm is O(n) where n is the length of the arrays, as we only iterate through the arrays once. The space complexity is O(1) as we use only a constant amount of extra space.

Solution Code

class Solution {
    public int minOperations(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int max1 = nums1[0], max2 = nums2[0];
        int idx1 = 0, idx2 = 0;
        
        for (int i = 0; i < n; i++) {
            if (nums1[i] > max1) {
                max1 = nums1[i];
                idx1 = i;
            }
            if (nums2[i] > max2) {
                max2 = nums2[i];
                idx2 = i;
            }
        }
        
        if (max1 == nums1[n - 1] && max2 == nums2[n - 1]) {
            return 0;
        }
        
        if (idx1 != n - 1) {
            nums1[idx1] = nums1[idx1] + nums2[idx1];
            nums2[idx1] = nums1[idx1] - nums2[idx1];
            nums1[idx1] = nums1[idx1] - nums2[idx1];
        }
        
        if (idx2 != n - 1) {
            nums1[idx2] = nums1[idx2] + nums2[idx2];
            nums2[idx2] = nums1[idx2] - nums2[idx2];
            nums1[idx2] = nums1[idx2] - nums2[idx2];
        }
        
        if (nums1[n - 1] == max1 && nums2[n - 1] == max2) {
            return idx1 == idx2 ? 1 : 2;
        }
        
        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2934 (Minimum Operations to Maximize Last Elements in Arrays)?

This page provides optimized solutions for LeetCode problem 2934 (Minimum Operations to Maximize Last Elements in Arrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2934 (Minimum Operations to Maximize Last Elements in Arrays)?

The time complexity for LeetCode 2934 (Minimum Operations to Maximize Last Elements in Arrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2934 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2934 in Java, C++, or Python.

Back to LeetCode Solutions