Maximize profitable pairs
Company: Akuna Capital
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Maximize profitable pairs states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= n <= 10^5
- -10^9 <= profits[i] <= 10^9
- -10^9 <= T <= 10^9
- Each index may be used in at most one pair (pairs are disjoint).
Examples
Input: ([1, 2, 3, 4, 5], 6)
Expected Output: 2
Explanation: Pairs (5,1) and (4,2) each sum to 6; 3 is left over.
Input: ([5, 5, 5, 5], 10)
Expected Output: 2
Explanation: Two pairs of (5,5), each summing to 10.
Hints
- Sort the array first. After sorting, the optimal strategy can be decided greedily from the two ends.
- Use two pointers: left at the smallest value, right at the largest. If their sum >= T, this is a valid pair — count it and move both inward.
- If profits[left] + profits[right] < T, then profits[left] cannot meet the threshold with ANY available partner (right is the largest left). Discard left (advance it) and try again.
- Greedy exchange argument: pairing the current smallest unpairable-or-pairable value with the largest never loses an achievable pairing, so this maximizes the count.