Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two integer arrays nums1 and nums2 both of the same length. The advantage of nums1 with respect to nums2 is the number of indices i for which nums1[i] > nums2[i].
Return any permutation of nums1 that maximizes its advantage with respect to nums2.
Example 1:
Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11] Output: [2,11,7,15]
Example 2:
Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11] Output: [24,32,8,12]
Constraints:
1 <= nums1.length <= 105nums2.length == nums1.length0 <= nums1[i], nums2[i] <= 109Problem summary: You are given two integer arrays nums1 and nums2 both of the same length. The advantage of nums1 with respect to nums2 is the number of indices i for which nums1[i] > nums2[i]. Return any permutation of nums1 that maximizes its advantage with respect to nums2.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Greedy
[2,7,11,15] [1,10,4,11]
[12,24,8,32] [13,25,32,11]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #870: Advantage Shuffle
class Solution {
public int[] advantageCount(int[] nums1, int[] nums2) {
int n = nums1.length;
int[][] t = new int[n][2];
for (int i = 0; i < n; ++i) {
t[i] = new int[] {nums2[i], i};
}
Arrays.sort(t, (a, b) -> a[0] - b[0]);
Arrays.sort(nums1);
int[] ans = new int[n];
int i = 0, j = n - 1;
for (int v : nums1) {
if (v <= t[i][0]) {
ans[t[j--][1]] = v;
} else {
ans[t[i++][1]] = v;
}
}
return ans;
}
}
// Accepted solution for LeetCode #870: Advantage Shuffle
func advantageCount(nums1 []int, nums2 []int) []int {
n := len(nums1)
t := make([][]int, n)
for i, v := range nums2 {
t[i] = []int{v, i}
}
sort.Slice(t, func(i, j int) bool {
return t[i][0] < t[j][0]
})
sort.Ints(nums1)
ans := make([]int, n)
i, j := 0, n-1
for _, v := range nums1 {
if v <= t[i][0] {
ans[t[j][1]] = v
j--
} else {
ans[t[i][1]] = v
i++
}
}
return ans
}
# Accepted solution for LeetCode #870: Advantage Shuffle
class Solution:
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1.sort()
t = sorted((v, i) for i, v in enumerate(nums2))
n = len(nums2)
ans = [0] * n
i, j = 0, n - 1
for v in nums1:
if v <= t[i][0]:
ans[t[j][1]] = v
j -= 1
else:
ans[t[i][1]] = v
i += 1
return ans
// Accepted solution for LeetCode #870: Advantage Shuffle
impl Solution {
pub fn advantage_count(mut nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
let n = nums1.len();
let mut idx = (0..n).collect::<Vec<usize>>();
idx.sort_by(|&i, &j| nums2[i].cmp(&nums2[j]));
nums1.sort();
let mut res = vec![0; n];
let mut left = 0;
let mut right = n - 1;
for &num in nums1.iter() {
if num > nums2[idx[left]] {
res[idx[left]] = num;
left += 1;
} else {
res[idx[right]] = num;
right -= 1;
}
}
res
}
}
// Accepted solution for LeetCode #870: Advantage Shuffle
function advantageCount(nums1: number[], nums2: number[]): number[] {
const n = nums1.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((i, j) => nums2[i] - nums2[j]);
nums1.sort((a, b) => a - b);
const ans = new Array(n).fill(0);
let left = 0;
let right = n - 1;
for (let i = 0; i < n; i++) {
if (nums1[i] > nums2[idx[left]]) {
ans[idx[left]] = nums1[i];
left++;
} else {
ans[idx[right]] = nums1[i];
right--;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.