Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
You may permute both arrays freely; you need a pairing where every `A[i] + B[i] >= k`. The hardest pair to satisfy is the smallest A paired with the smallest B, so protect the small A values by giving them the largest B values. Sort A ascending and B descending, then check every index: if all `A[i] + B[i] >= k` it's possible ('YES'), otherwise no pairing works ('NO'). O(n log n) from the sorts.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you realise both arrays are free to permute, and that pairing smallest-with-largest maximises the weakest pair sum — then a single linear feasibility check decides YES/NO.
Where candidates slip
Sorting both arrays the same direction (smallest with smallest, which minimises the worst pair), or trying to brute-force permutations.
How to stand out
Prove via exchange/rearrangement: if a small A is paired with a small B while a large A has a large B, swapping the B's never lowers the minimum pair sum — so opposite sorting is optimal for the min-objective.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewArrays A, B, threshold kAll A[i] + B[i] >= k?YesNoExplore the full treeCollapse the tree
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 2
Brute Force — Try Every Pairing
1Hint 1Explore hintHide hint
Since both arrays can be permuted, fixing A and permuting B covers all pairings. Try every permutation of B against A.
2Hint 2Explore hintHide hint
For each permutation, check that all A[i] + B[i] >= k. If any permutation passes, the answer is YES.
3Hint 3Explore hintHide hint
There are n! permutations and each check is O(n) → useless beyond tiny n. It exists only to motivate the opposite-sort greedy.
A · Core idea
Hold A fixed and permute B — that explores every possible pairing. For each arrangement, check whether all A[i] + B[i] >= k; if any arrangement works, output YES.
Why it's correct. Permuting one array against a fixed other enumerates all n! pairings, so if a feasible pairing exists, this finds it.
Why it's hopeless. n! permutations — even n = 12 is half a billion. With n up to 1000 it never runs.
The structure it reveals. Among all pairings, the one that gives the largest possible smallest sum is small-A-with-large-B. If that arrangement can't clear k at every index, no arrangement can; if it can, we're done. So we don't need to try all n! — just the opposite-sorted one.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
A = [1, 2, 2, 1], B = [3, 3, 3, 4], k = 5B = [4,3,3,3]
sums 5,5,5,4 → index 3 fails
not feasible yetNOC · Code with teaching comments
Reference implementation previewpublic String twoArraysBrute(int k, int[] A, int[] B) { Integer[] b = new Integer[B.length]; for (int i = 0; i < B.length; i++) b[i] = B[i]; return permute(A, b, 0, k) ? "YES" : "NO"; }Explore the full codeHide the full code
D · Why it works and complexityO(n! · n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
Feasibility hinges entirely on the pairing that maximises the minimum pair sum. That pairing is opposite-sorted (small A with large B), so one arrangement decides the whole question.
ComplexityO(n! · n) time, O(n) space — Fixing A and permuting B gives n! pairings; verifying each is O(n). The factorial dominates.
E · Edge cases and pitfalls3n! blow-up — only feasible for n ≤ ~10.Explore edge casesHide edge cases
Pitfalls
- n! blow-up — only feasible for n ≤ ~10.
- Permuting both arrays (redundant) instead of fixing one and permuting the other.
- Forgetting that ALL pairs must satisfy the threshold, not just one.
Approach 2 of 2 · Optimal
Greedy — Sort A Ascending, B Descending, Check Every Pair (Optimal)
1Hint 1Explore hintHide hint
The pair most at risk of falling below k is the smallest A with the smallest B. Don't let that happen — give small A values large B values.
2Hint 2Explore hintHide hint
Sort A ascending and B descending, then pair by index: smallest A with largest B, next-smallest with next-largest, and so on.
3Hint 3Explore hintHide hint
Scan once: if any A[i] + B[i] < k the answer is NO; if all reach k it's YES. This pairing maximises the minimum pair sum.
A · Core idea
Sort A ascending and B descending, pair them index-by-index, and check that every A[i] + B[i] >= k. If they all do, output YES; otherwise NO.
Why opposite sorting maximises the minimum pair sum — the exchange argument. Consider any pairing and look at two pairs (a₁, b₁) and (a₂, b₂) with a₁ < a₂ but b₁ < b₂ (the smaller A got the smaller B — a 'same-direction' pairing). Their two sums are a₁ + b₁ and a₂ + b₂, whose minimum is a₁ + b₁ (the smallest A with the smallest B). Swap the B values to get (a₁, b₂) and (a₂, b₁). The new minimum of the two sums is min(a₁ + b₂, a₂ + b₁). Both of these are ≥ a₁ + b₁: a₁ + b₂ > a₁ + b₁ since b₂ > b₁, and a₂ + b₁ > a₁ + b₁ since a₂ > a₁. So the swap never lowers the minimum pair sum and can only raise it. Repeatedly applying it sorts A and B into opposite orders — proving that opposite sorting yields the largest achievable minimum pair sum.
Why that settles feasibility. A feasible permutation exists iff some pairing makes the minimum pair sum ≥ k. Opposite sorting achieves the maximum possible minimum, so if even it falls short at some index, no pairing can succeed (NO); if it clears k everywhere, we have an explicit witness (YES).
Why a single failing index is decisive. The constraint is universal — every pair must reach k. The opposite-sorted pairing already gives each index the best partner it can get for the min-objective, so one sub-k sum here means that element simply cannot be lifted to k by any reordering.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
A = [2, 1, 3], B = [7, 8, 9], k = 10Array state
i=0
1 + 9 = 10 ≥ 10 ✓
YESC · Code with teaching comments
D · Why it works and complexityO(n log n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
This is the rearrangement inequality applied to the MINIMUM (not the sum): to maximise the smallest paired value, sort the two sequences oppositely. The reusable kernel — 'make every pair clear a bar' → defend the weakest A with the strongest B, then one linear check decides feasibility.
ComplexityO(n log n) time, O(1) space — Two sorts cost O(n log n) and dominate; the single verification pass is O(n) with only a boolean flag → O(1) extra space.
E · Edge cases and pitfalls6n = 1 — Single pair: YES iff A[0] + B[0] >= k.Explore edge casesHide edge cases
Edge cases handled
n = 1Single pair: YES iff A[0] + B[0] >= k.all equal, A=[1,1,1], B=[1,1,1], k=3Every pair sums to 2 < 3 → NO regardless of order.very large values near 10⁹A[i] + B[i] can exceed int range — compute the sum as long.
Pitfalls
- Sorting both arrays the same direction (smallest with smallest) — minimises the worst pair, the opposite of what you want.
- Returning YES on the first passing pair instead of requiring ALL pairs to pass.
- Integer overflow: 10⁹ + 10⁹ exceeds int; sum as long before comparing to k.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String twoArrays(int k, int[] A, int[] B) { int n = A.length; Integer[] a = new Integer[n]; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = A[i]; b[i] = B[i]; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Same-direction sorting
Pairing smallest A with smallest B minimises the worst pair sum — the opposite of the goal. Sort B descending against A ascending.
Explore the code comparisonHide the code comparison
Same-direction sorting
Pairing smallest A with smallest B minimises the worst pair sum — the opposite of the goal. Sort B descending against A ascending.
Don't write this
A = sorted(A)
B = sorted(B) # both ascending → small + small
return "YES" if all(a + b >= k for a, b in zip(A, B)) else "NO"Write this instead
A = sorted(A)
B = sorted(B, reverse=True)
return "YES" if all(a + b >= k for a, b in zip(A, B)) else "NO"2Returning YES too early
The condition is universal — every pair must reach k. Returning YES on the first passing pair ignores later failures.
Explore the code comparisonHide the code comparison
Returning YES too early
The condition is universal — every pair must reach k. Returning YES on the first passing pair ignores later failures.
Don't write this
for (int i = 0; i < n; i++) {
if ((long) a[i] + b[i] >= k) return "YES"; // wrong: only one pair checked
}
return "NO";Write this instead
for (int i = 0; i < n; i++) {
if ((long) a[i] + b[i] < k) return "NO";
}
return "YES";3Integer overflow on the sum
A[i] and B[i] can each be 10⁹; their int sum overflows. Add as long before comparing to k.
Explore the code comparisonHide the code comparison
Integer overflow on the sum
A[i] and B[i] can each be 10⁹; their int sum overflows. Add as long before comparing to k.
Don't write this
if (a[i] + b[i] < k) return "NO"; // int + int can overflowWrite this instead
if ((long) a[i] + b[i] < k) return "NO";Revise
Carry the pattern into the interview
Pattern to remember
Greedy opposite-sort pairing: smallest with largest to maximise the minimum pair sum
Code pattern to memorize
sort A ascending
sort B descending
for i in 0..n-1:
if A[i] + B[i] < k: return "NO"
return "YES"Rules to remember
- 1You may reorder BOTH arrays independently — the only thing that matters is which A pairs with which B.
- 2Pair the smallest A with the largest B (ascending A against descending B). This maximises the smallest pair sum.
- 3If even this optimal pairing has any
A[i] + B[i] < k, no permutation can satisfy the constraint → 'NO'. - 4All n pair sums must reach k; a single failing index sinks the whole answer.
- 5Both arrays have the same length n; each A element pairs with exactly one B element.
Use this pattern when
- Pair two equal-length arrays to satisfy a per-pair threshold on every pair
- Maximising the MINIMUM of paired sums (or products) by choosing the pairing
- A feasibility (yes/no) question about whether some pairing meets all constraints
- Opposite sorting is the natural move (rearrangement inequality, min-side)
Choose another pattern when
- You may pick a subset, not pair all elements → selection problem, not full pairing
- The threshold applies to the SUM of all pairs, not each pair → totals, sort same direction
- Only one array can be permuted with the other fixed → constrained assignment
- Pairs interact beyond their own sum → matching / flow
Interview takeaway
To make every pair clear a threshold, defend the weakest links: give the smallest A values the biggest B values. Sort oppositely and check the per-index minimum. If the best pairing fails anywhere, every pairing does — that's the rearrangement-inequality argument for maximising the minimum.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternRearrangement-inequality greedy for the MINIMUM: to make every pair clear a threshold, pair the smallest of one array with the largest of the other (opposite sort), which maximises the smallest pair sum. One linear feasibility check then decides YES/NO.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Greedy FloristRearrangement inequality on the SUM — sort oppositely to minimise a weighted total.
- Beautiful PairsAnother two-array greedy, solved by counting rather than pairing-by-sort.
- Two City SchedulingComing soonPair/assign by a sorted difference key — greedy assignment under a balance constraint.
- Priyanka and ToysSorting unlocks a clean linear pass, as the opposite-sort does here.