FastPrepReaching Points with Perfect-Square Obstacles

Reaching Points with Perfect-Square Obstacles

IMC Trading logoIMC Trading● MediumFULLTIMEINTERNNEW GRADOA
Learn

Problem statement

A bot starts at the nonnegative integer coordinate (startX, startY) and wants to reach (targetX, targetY). A positive constant c is fixed for the entire journey.

From a coordinate (x, y), the bot may make exactly one of these moves:

  1. Move to (x + y, y).
  2. Move to (x, x + y).
  3. Move to (x + c, y + c).

A coordinate is forbidden when x + y is a perfect square. The bot may never occupy a forbidden coordinate. This rule also applies to the starting and target coordinates.

Return "Yes" if a sequence of legal moves reaches the target. Otherwise, return "No".

Function

canReach(c: int, startX: int, startY: int, targetX: int, targetY: int) → String

Examples

Example 1

c = 1startX = 2startY = 1targetX = 3targetY = 5return = "Yes"

The bot can move from (2, 1) to (3, 2) using the third move, then to (3, 5) using the second move. Neither visited sum is a perfect square.

Example 2

c = 2startX = 2startY = 7targetX = 10targetY = 12return = "No"

The starting sum is 2 + 7 = 9, a perfect square, so no path is allowed.

Constraints

  • 0 <= startX, startY, targetX, targetY <= 1000
  • 1 <= c <= 15
  • Coordinates never decrease. On an axis, a move can leave the position unchanged.
  • If either endpoint's coordinate sum is a perfect square, the answer is "No". This includes sum 0.

More IMC Trading problems

See IMC Trading hiring insights
public String canReach(int c, int startX, int startY, int targetX, int targetY) {
    // Write your solution here.
}
c1
startX2
startY1
targetX3
targetY5
expected"Yes"
Checking account…