FastPrepMaximum Reachable Value Within a Step Budget

Maximum Reachable Value Within a Step Budget

Adobe logoAdobe● EasyINTERNOA
Learn

Problem statement

You start at coordinate x on the integer number line and must finish at coordinate y. Each step moves exactly 1 unit left or right.

You may use at most steps steps. Return the maximum coordinate that can be visited by any valid walk that ends at y. If y cannot be reached within the step budget, return -1.

Function

maximumReachableValue(x: long, y: long, steps: long) → long

Examples

Example 1

x = 3y = 4steps = 5return = 6

The walk 3,4,5,6,5,4 uses 5 steps, ends at 4, and reaches coordinate 6.

Example 2

x = 2y = 8steps = 4return = -1

Reaching 8 from 2 requires at least 6 steps, so the budget is insufficient.

Example 3

x = 10y = 4steps = 8return = 11

Moving from 10 to 4 needs 6 steps. The two extra steps allow a detour to 11 before returning through 10 and continuing to 4.

Constraints

  • 0 <= x <= 10^9.
  • 0 <= y <= 10^9.
  • 0 <= steps <= 2 * 10^9.
  • The result fits in a signed 64-bit integer.

Source note: These two source-faithful panels preserve the complete March 12 Adobe OA task and its parity-aware solution notes.

More Adobe problems

See Adobe hiring insights
public long maximumReachableValue(long x, long y, long steps) {
    // Write your code here.
}
x3
y4
steps5
expected6
Checking account…