Find minimal Ai2 Bi2 when A and B are sorted
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If the goal is to minimize A[i]^2 + B[j]^2, the key observation is that i and j are independent choices. That makes the problem much simpler than many first attempts suggest: you do not need a two-pointer search over pairs, because the minimum sum is just the minimum squared value from A plus the minimum squared value from B.
Why the Problem Separates Cleanly
The expression is:
A[i]^2 + B[j]^2
There is no cross-term involving both arrays together. That means the best i does not depend on which j you pick, and the best j does not depend on which i you pick.
So the global minimum is:
- find the element of
Awhose absolute value is smallest - find the element of
Bwhose absolute value is smallest - square them and add the results
This is the whole optimization.
If A[i] is not the smallest-magnitude value in A, replacing it with a smaller-magnitude value lowers or preserves A[i]^2 while leaving B[j]^2 unchanged. The same logic applies to B.
What Sorted Order Gives You
Because the arrays are sorted, the element with minimum absolute value must be near where the array crosses zero. That lets you find the best value faster than scanning every element.
There are two common cases:
- if all values are non-negative, the answer is at the first element
- if all values are non-positive, the answer is at the last element
- if the array crosses zero, the best value is one of the two entries around the insertion point of zero
That means binary search is enough.
A Simple Python Solution
The function below finds the smallest squared value in one sorted array, then applies it to both arrays.
For this example, the best choices are 2 from A and -1 from B, so the minimum is 2^2 + (-1)^2 = 5.
Complexity
Using binary search, each array takes O(log n) and O(log m) time respectively. The total cost is therefore O(log n + log m).
If the arrays were not sorted, the problem would still be easy, but you would scan once through each array to find the smallest absolute value, giving O(n + m) time.
That is still much better than comparing all pairs, which would cost O(n * m) and solves a harder problem than you actually have.
Why a Two-Pointer Pair Search Is Unnecessary
Two-pointer methods are useful when the arrays interact through a combined ordering condition, such as finding a pair with sum closest to a target. Here, there is no such dependency.
A pair-search algorithm adds complexity without adding value because the best contribution from A can be chosen independently of B.
That is a good interview lesson in itself: simplify the math before reaching for a more elaborate traversal pattern.
Edge Cases
A few edge cases are worth handling explicitly:
- if either array contains
0, that array contributes0 - duplicate values do not matter; any minimum-magnitude representative is fine
- arrays must be non-empty or the problem is undefined
If the task also asks for the actual indices rather than only the minimum value, return the index of the chosen element closest to zero in each array.
Common Pitfalls
Treating the problem like a general pair-optimization problem is the most common mistake. Here the terms are separable.
Assuming the smallest numeric value gives the smallest square is another mistake. With squares, -100 is worse than 2 even though -100 is numerically smaller.
For sorted arrays, scanning every pair is unnecessary and overcomplicates the solution.
Finally, remember that what matters is absolute value, not raw ordering around the left edge of the array.
Summary
- '
A[i]^2 + B[j]^2separates into two independent minimization problems' - the minimum is obtained by choosing the smallest-magnitude value from each array
- sorted order lets you find that value with binary search near zero
- the optimal time complexity is
O(log n + log m)for sorted inputs - a two-pointer search over pairs is solving a harder problem than the math requires

