Advantage Shuffle
Problem Statement
You have two lists of the same length, nums1 and nums2. Think of nums1 as your team and nums2 as the opponents. Position i is a fight between your value and the opponent at position i.
You may reorder your team in any way, but you must keep every value. After the reorder, a fight is a win when your value is strictly bigger than the opponent's. The advantage is the number of wins.
Return a reordering of nums1 with the largest advantage. If several reorderings tie, any of them is fine.
Two arrays nums1 and nums2 of the same length. Every value is 0 or more.
A list: a reordering of nums1 that gives the largest advantage against nums2.
All four fights are wins: 2 > 1, 11 > 10, 7 > 4 and 15 > 11. That is the most possible.
Three fights are wins: 24 > 13, 32 > 25 and 12 > 11. The 32 opponent can't be beaten by anything we have, so the 8 goes there and the fight is lost.
- • 1 ≤ nums1.length ≤ 10^5
- • nums2.length = nums1.length
- • 0 ≤ nums1[i], nums2[i] ≤ 10^9
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try all orders of nums1 and keep the one with the most wins
For each nums2 in its given order, scan the values left for the smallest one that beats it
Sort our values. Go through the opponents strongest first: our strongest wins if it can, otherwise our weakest is given up
Generate every way to reorder nums1. For each one, count the positions where our value beats nums2 at that position. Keep the reordering with the most wins.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.