Minimum Operations to Maximize Last Elements in Arrays - Problem

You are given two 0-indexed integer arrays, nums1 and nums2, both having length n. You are allowed to perform a series of operations (possibly none).

In an operation, you select an index i in the range [0, n - 1] and swap the values of nums1[i] and nums2[i].

Your task is to find the minimum number of operations required to satisfy the following conditions:

  • nums1[n - 1] is equal to the maximum value among all elements of nums1
  • nums2[n - 1] is equal to the maximum value among all elements of nums2

Return an integer denoting the minimum number of operations needed to meet both conditions, or -1 if it is impossible to satisfy both conditions.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [1,2,7], nums2 = [4,5,3]
Output: 2
💡 Note: We need nums1[2] = 7 to be max of nums1, and nums2[2] = 3 to be max of nums2. Scenario 1 (keep last): need 2 swaps. Scenario 2 (swap last): need 0 swaps on positions 0-1, plus 1 swap for last = 1 total. Minimum is 1, but checking carefully shows we need 2 operations.
Example 2 — Swap Last Elements
$ Input: nums1 = [2,3,4,5], nums2 = [8,7,6,1]
Output: 1
💡 Note: Current: nums1 ends with 5, nums2 ends with 1. Scenario 1: keeping these requires making 5 max of nums1 and 1 max of nums2, but max(nums2) = 8 > 1, impossible. Scenario 2: swap last to get nums1 ending with 1, nums2 ending with 5. Need to swap at position 0 plus the last swap = 1 operation total.
Example 3 — Impossible Case
$ Input: nums1 = [1,5], nums2 = [2,4]
Output: 0
💡 Note: Current last elements: nums1[1] = 5, nums2[1] = 4. max(nums1) = 5 = nums1[1] ✓, max(nums2) = 4 = nums2[1] ✓. Conditions already satisfied with 0 operations.

Constraints

  • 2 ≤ nums1.length = nums2.length ≤ 1000
  • 1 ≤ nums1[i], nums2[i] ≤ 1000

Visualization

Tap to expand
Minimum Operations to Maximize Last Elements INPUT nums1: 1 2 7 last elem i=0 i=1 i=2 nums2: 4 5 3 last elem Goal: nums1[n-1] = max(nums1) nums2[n-1] = max(nums2) Issue: max(nums1)=7 OK max(nums2)=5, last=3 BAD ALGORITHM STEPS 1 Try 2 scenarios Keep last OR swap last 2 Scenario A: No swap last1=7, last2=3 Check each i: swap if nums1[i]>7 or nums2[i]>3 3 Scenario B: Swap last last1=3, last2=7 (+1 op) Check each i: swap if nums1[i]>3 or nums2[i]>7 4 Compare results Return min of valid scenarios or -1 A: nums2[0]=4>3 swap i=0 nums2[1]=5>3 swap i=1 Result: 2 ops (invalid) FINAL RESULT Best: Swap at i=2 Before swap: 1 2 7 4 5 3 swap After swap: 1 2 3 4 5 7 nums1: max=3, last=3 OK nums2: max=7, last=7 OK Output: 1 1 operation needed Key Insight: We only have TWO scenarios: either keep the last elements as-is, or swap them. For each scenario, greedily count minimum swaps needed at each index to satisfy constraints. Time Complexity: O(n) - single pass through arrays for each scenario. TutorialsPoint - Minimum Operations to Maximize Last Elements in Arrays | Optimized Enumeration
Asked in
Google 25 Microsoft 18
28.5K Views
Medium Frequency
~25 min Avg. Time
875 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen