Reorder One Array to Win as Many Position Comparisons as Possible

Quick Overview

Reorder One Array to Win as Many Position Comparisons as Possible. Rearrange nums1 to maximize the number of indices i where the rearranged value is strictly greater than nums2[i].

Reorder One Array to Win as Many Position Comparisons as Possible

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Rearrange `nums1` to maximize the number of indices i where the rearranged value is strictly greater than `nums2[i]`. Return any maximizing permutation of nums1; nums2 stays in its original order. ```cpp class Solution { public: vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2); }; ``` ### Examples **Example 1:** ```text Input: nums1 = [3,5,8], nums2 = [4,7,2] Output: [5,8,3] Explanation: All three comparisons are wins, so no permutation can improve the count. ``` **Example 2:** ```text Input: nums1 = [1,2,2], nums2 = [2,1,3] Output: [1,2,2] Explanation: The only beatable target position contains 1; either copy of 2 can beat it. No source value exceeds the targets 2 or 3, so at most one position can be won. The returned permutation achieves that one win. ``` ### Constraints - The arrays have a common length n, with `1 <= n <= 100000`. - Each element in either array is an integer between0 and1000000000, inclusive. ### Contract considerations Advantage counts strict wins, not the sum of numeric differences. Any maximum-win permutation is accepted. ```hint Read the boundary condition Which target should receive a source value that cannot beat even the smallest unmatched target? ``` ### What a Strong Answer Covers Advantage counts strict wins, not the sum of numeric differences. Any maximum-win permutation is accepted. ### Follow-up Questions Optional practice extension, separate from the original task: How could you validate optimality independently of the particular returned permutation?

Overview: Reorder One Array to Win as Many Position Comparisons as Possible. Rearrange nums1 to maximize the number of indices i where the rearranged value is strictly greater than nums2[i].

Squarepoint logo
Squarepoint
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Rearrange nums1 to maximize the number of indices i where the rearranged value is strictly greater than nums2[i]. Return any maximizing permutation of nums1; nums2 stays in its original order.

class Solution {
public:
    vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2);
};

Examples

Example 1:

Input: nums1 = [3,5,8], nums2 = [4,7,2]
Output: [5,8,3]
Explanation: All three comparisons are wins, so no permutation can improve the count.

Example 2:

Input: nums1 = [1,2,2], nums2 = [2,1,3]
Output: [1,2,2]
Explanation: The only beatable target position contains 1; either copy of 2 can beat it. No source value exceeds the targets 2 or 3, so at most one position can be won. The returned permutation achieves that one win.

Constraints

  • The arrays have a common length n, with 1 <= n <= 100000 .
  • Each element in either array is an integer between0 and1000000000, inclusive.

Contract considerations

Advantage counts strict wins, not the sum of numeric differences. Any maximum-win permutation is accepted.

What a Strong Answer Covers Guidance

Advantage counts strict wins, not the sum of numeric differences. Any maximum-win permutation is accepted.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How could you validate optimality independently of the particular returned permutation?