FastPrepSquared Frobenius Norm of a Reflect-and-Stretch Map

Squared Frobenius Norm of a Reflect-and-Stretch Map

Walleye Capital logoWalleye Capital● EasyFULLTIMEOA

Problem statement

Let L be the line through the origin with direction vector (dx, dy). A linear transformation T from R^2 to R^2 leaves every point on L unchanged.

For any point P not on L, its image T(P):

  • has the same orthogonal projection onto L as P,
  • lies on the opposite side of L, and
  • is k times as far from L as P is.

Let A be the 2 x 2 matrix of T. Return the squared Frobenius norm of A, which is the sum of the squares of its four entries. The answer is always a whole number.

The original question uses the line y = (3/4)x and twice the distance, which is the input dx = 4, dy = 3, k = 2.

Function

squaredFrobeniusNorm(dx: int, dy: int, k: int) → long

Examples

Example 1

dx = 4dy = 3k = 2return = 5

The projection onto L is P = [[16, 12], [12, 9]] / 25. Keeping the part along L and sending the perpendicular part to -2 times itself gives A = P - 2(I - P) = [[-2, 36], [36, -23]] / 25.

The squared entries sum to (4 + 1296 + 1296 + 529) / 625 = 3125 / 625 = 5.

Example 2

dx = 1dy = 0k = 1return = 2

L is the x-axis and k = 1, so T is the reflection (x, y) -> (x, -y). Its matrix is [[1, 0], [0, -1]], and 1 + 1 = 2.

Example 3

dx = 0dy = -5k = 3return = 10

L is the y-axis. T keeps y and sends x to -3x, so A = [[-3, 0], [0, 1]] and 9 + 1 = 10.

Constraints

  • -1000 <= dx, dy <= 1000
  • dx and dy are not both 0
  • 1 <= k <= 10^6

More Walleye Capital problems

See Walleye Capital hiring insights
Saved
public long squaredFrobeniusNorm(int dx, int dy, int k) {
    // Write your code here.
}
dx4
dy3
k2
expected5
Checking account…