algorithm
optimization
computational mathematics
sorted arrays
problem solving

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 A whose absolute value is smallest
  • find the element of B whose 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.

python
1from bisect import bisect_left
2
3
4def min_square(sorted_values):
5    pos = bisect_left(sorted_values, 0)
6    candidates = []
7
8    if pos < len(sorted_values):
9        candidates.append(sorted_values[pos])
10    if pos > 0:
11        candidates.append(sorted_values[pos - 1])
12
13    best = min(candidates, key=lambda x: x * x)
14    return best * best
15
16
17def min_sum_of_squares(A, B):
18    return min_square(A) + min_square(B)
19
20
21A = [-10, -3, 2, 7]
22B = [-8, -1, 4, 9]
23print(min_sum_of_squares(A, B))

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 contributes 0
  • 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]^2 separates 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

Course illustration
Course illustration

All Rights Reserved.