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.
Input: ([1, 1, 1, 1], 10)
Expected Output: 0
Explanation: No pair reaches 10.
Input: ([-3, -2, 4, 5], 1)
Expected Output: 2
Explanation: Sorted [-3,-2,4,5]: (-3,5)=2>=1 and (-2,4)=2>=1, so 2 pairs.
Input: ([], 5)
Expected Output: 0
Explanation: Empty array: no pairs.
Input: ([7], 3)
Expected Output: 0
Explanation: A single element cannot form a pair.
Input: ([0, 0, 0], 0)
Expected Output: 1
Explanation: Pair (0,0)=0>=0; the third 0 is unpaired (odd n).
Input: ([10, -10, 3, 3], 0)
Expected Output: 2
Explanation: (10,-10)=0 and (3,3)=6, both >= 0.
Input: ([1, 2, 3, 4, 5, 6], -100)
Expected Output: 3
Explanation: Threshold is very small, so every value pairs: 3 pairs from 6 elements.
Input: ([2, 2, 2, 2], 5)
Expected Output: 0
Explanation: Max pair sum is 4 < 5, so no pairs.
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.