FastPrepReachable Transfer Positions

Reachable Transfer Positions

Citadel logoCitadel● MediumNEW GRADPHONE SCREEN

Problem statement

You are given target positions and allowed trades. Each position is an integer pair [x,y]. Each trade is a vector [-d,d], which transfers an integer amount between the two coordinates without changing their sum.

Choose one shared integer starting position. You may apply any allowed trade or its negative any number of times, including zero. Return true if every target position can be reached from that same starting position, and false otherwise. Each target may use its own sequence of trades.

These repeated, bidirectional, zero-sum transfer rules define this practice version. If there are no target positions, return true. Zero trades are allowed; if every trade is zero or the trade list is empty, all target positions must be identical.

Function

canReachPositions(positions: int[][], trades: int[][]) → boolean

Examples

Example 1

positions = [[-10,10],[-20,20]]trades = [[-1,1],[-5,5]]return = true

Start at [-10,10]. Applying [-5,5] twice reaches [-20,20]. The starting position itself needs no trade, so both targets are reachable.

Example 2

positions = [[0,4],[3,1]]trades = [[-2,2]]return = false

Both targets have coordinate sum 4, but every allowed trade changes the first coordinate by an even number. Their first coordinates differ by 3, so no shared start can reach both.

Example 3

positions = [[1,4],[3,3]]trades = [[-1,1]]return = false

The coordinate sums are 5 and 6. Every trade preserves this sum, so one starting position cannot reach both targets.

Constraints

  • 0 <= positions.length <= 5000.
  • 0 <= trades.length <= 5000.
  • Every position and trade contains exactly two integers.
  • Every coordinate is between -10^9 and 10^9.
  • The two coordinates of every trade sum to zero.

More Citadel problems

See Citadel hiring insights
Saved
public boolean canReachPositions(int[][] positions, int[][] trades) {
    // Write your code here.
}
positions[[-10,10],[-20,20]]
trades[[-1,1],[-5,5]]
expectedtrue
Checking account…