MediumArrayTwo PointersGreedySortingUpdated Sep 2026

870. Advantage Shuffle

Asked at Walmart

Difficulty

Medium

Time

O(n log n)

Space

O(n)

Pattern

Array

Companies

1

Problem

You get two integer arrays, nums1 and nums2, of the same length. The advantage of nums1 over nums2 is the count of positions i where nums1[i] > nums2[i]. The comparison must be strictly greater — equal values do not count as a win.

You may reorder nums1 in any way you like. nums2 must stay exactly as it is. Return any permutation of nums1 that gives the largest possible advantage. If several permutations tie, any one of them is accepted.

Think of it as a card game: nums2 is the opponent's fixed hand, and you want to line up your cards so you beat as many of theirs as possible.

Line up your cards to beat the opponent

nums2 (fixed)1104110123The opponent's hand stays in this exact order.nums1 (we reorder)2117150123Every position wins: 2>1, 11>10, 7>4, 15>11 —advantage 4.
nums2 is fixed. We reorder nums1 so that as many positions as possible have nums1[i] > nums2[i].

Constraints

  • ●1 <= nums1.length <= 10^5
  • ●nums2.length == nums1.length
  • ●0 <= nums1[i], nums2[i] <= 10^9

Examples

Example 1
Inputnums1 = [2,7,11,15], nums2 = [1,10,4,11]
Output[2,11,7,15]
WhyCompare position by position: 2>1, 11>10, 7>4, 15>11. All four positions are wins, so the advantage is 4 — the maximum possible.
Example 2
Inputnums1 = [12,24,8,32], nums2 = [13,25,32,11]
Output[24,32,8,12]
Why24>13 and 32>25 are wins. 8<32 and 12>11, so 12 wins too. That is 3 wins. The remaining card 8 cannot beat 32, so one loss is unavoidable.

Asked At

1 companies
CompanyDifficulty
WalmartMediumView all Walmart questions →

How to Think About It

1

Brute force: try all permutations of nums1 against nums2. That's O(n!) - completely impractical. We need a greedy strategy.

2

Key insight: sort both arrays. For each element in nums2 (from largest to smallest), assign the smallest element in nums1 that beats it. If no element can beat it, assign the smallest remaining element as a sacrifice.

3

Why this greedy works: by matching the smallest winning element against each opponent, you preserve larger elements for harder opponents. This is the optimal strategy - it never wastes a big element on an easy win.

4

Implementation: sort nums1 ascending. Sort nums2 with indices. Use two pointers on nums1 (lo=0, hi=n-1) and iterate nums2 from largest to smallest. If nums1[hi] > nums2[j], assign nums1[hi] (win). Otherwise assign nums1[lo] (sacrifice).

5

Edge cases: all elements in nums1 are smaller than nums2 (all sacrifices), all elements in nums1 are larger (all wins), equal values (not a win - need strictly greater).

Optimal Approach

Step 1: Sort nums1. Sort nums2 with original indices.
Step 2: Initialize lo = 0, hi = n-1 on nums1.
Step 3: Iterate nums2 from largest to smallest:

  • If nums1[hi] > nums2[j] element: assign nums1[hi] to that position (win). Decrement hi.
  • Otherwise: assign nums1[lo] to that position (sacrifice). Increment lo.
    Step 4: Place assigned values back into the result array at the original indices of nums2.

Walkthrough with nums1 = [2,7,11,15] nums2 = [1,10,4,11]:

  • Sort nums1: [2,7,11,15].
  • Sorted nums2: [(1,0), (4,2), (10,1), (11,3)].
  • Largest nums2=11: 15>11, win. result[3]=15.
  • nums2=10: 11>10, win. result[1]=11.
  • nums2=4: 7>4, win. result[2]=7.
  • nums2=1: 2>1, win. result[0]=2.
  • Result: [2,7,11,15].

Time: O(n log n) for sorting. Space: O(n) for the result array.

Greedy: biggest opponent first

nums1 sorted2711150123lohiTwo pointers: lo is the smallest card, hi is thelargest card.nums2 sorted desc1110410123Process opponents from largest to smallest,remembering their original index.15 > 11 → win, place 1511 > 10 → win, place 117 > 4 → win, place 72 > 1 → win, place 2Each opponent is beaten by the smallest card thatstill wins, so no big card is wasted.if nums1[hi] > opponent: use hi, hi--else: sacrifice nums1[lo], lo++
Sort nums1, then answer the largest opponent with your largest card if it wins, otherwise sacrifice your smallest card.
ComplexityTime O(n log n)Space O(n)Sorting both arrays costs O(n log n); the single pass over the sorted values is linear, and the result array needs O(n) extra space.

What Trips People Up in Real Interviews

1

Trying to sort both arrays and compare element-by-element. That does not maximize wins - you need to match each nums2 element against the best available nums1 element.

2

Forgetting that equal values are not wins. The condition is strictly greater: nums1[i] > nums2[i]. If nums1[i] == nums2[i], it is not an advantage.

3

Assigning the largest nums1 element to the smallest nums2 element. That wastes big elements. Instead, assign the smallest winning element to each nums2 element to preserve big ones for tough opponents.

4

Losing track of original indices. After sorting nums2, you must remember where each element came from so you can place the assigned value at the correct position in the result.

5

Not handling the sacrifice case. When no element in nums1 can beat the current nums2 element, assign the smallest remaining element. It will lose anyway, so sacrifice it and save bigger elements for winnable matchups.

Solution Code

O(n log n) time
def advantageCount(nums1, nums2):
    nums1.sort()
    sorted_nums2 = sorted((v, i) for i, v in enumerate(nums2))
    result = [0] * len(nums1)
    lo, hi = 0, len(nums1) - 1
    for val, idx in reversed(sorted_nums2):
        if nums1[hi] > val:
            result[idx] = nums1[hi]
            hi -= 1
        else:
            result[idx] = nums1[lo]
            lo += 1
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Advantage Shuffle problem?

Given two arrays nums1 and nums2 of equal length, rearrange nums1 to maximize the number of positions where nums1[i] > nums2[i]. If not possible, put any remaining elements there. This is a greedy matching problem that tests your ability to optimize element assignment.

How do you solve Advantage Shuffle?

The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Advantage Shuffle?

Advantage Shuffle is asked at Walmart. It is a medium difficulty problem.

What are common mistakes on Advantage Shuffle?
  • Trying to sort both arrays and compare element-by-element. That does not maximize wins - you need to match each nums2 element against the best available nums1 element.
  • Forgetting that equal values are not wins. The condition is strictly greater: nums1[i] > nums2[i]. If nums1[i] == nums2[i], it is not an advantage.
  • Assigning the largest nums1 element to the smallest nums2 element. That wastes big elements. Instead, assign the smallest winning element to each nums2 element to preserve big ones for tough opponents.
  • Losing track of original indices. After sorting nums2, you must remember where each element came from so you can place the assigned value at the correct position in the result.
  • Not handling the sacrifice case. When no element in nums1 can beat the current nums2 element, assign the smallest remaining element. It will lose anyway, so sacrifice it and save bigger elements for winnable matchups.