Reach The Point
Updated
Problem statement
Suppose there is a grid consisting of an infinite number of cells and you are standing on cell (0, 0).
Let your move strength be K, which is initially set to 1.
In one move you can perform one of the following operations:
- Move horizontally, i.e. go from
(x, y)to(x + K, y). - Move vertically, i.e. go from
(x, y)to(x, y + K). - Increase the value of
Kby1.
K never goes down, and you can move as many times as you like with the same K.
Find the minimum number of moves to reach the destination cell (a, b).
Complete the function minimumMoves in the editor.
minimumMoves has the following parameters:
int a: the x-coordinate of the destinationint b: the y-coordinate of the destination
The function returns an int: the minimum number of moves needed to reach (a, b).
Function
minimumMoves(a: int, b: int) → intExamples
Example 1
a = 1b = 6return = 5For the first test case, First move to (1,0), then increase the value of K to 2, and then move three times to reach (1,6). Hence, it takes a total of 5.
Example 2
a = 1b = 1return = 2Keep K = 1. Move right once to (1, 0), then up once to (1, 1). That is 2 moves, and no single move can change both coordinates, so 2 is the minimum.
Constraints
1 ≤ a, b ≤ 109
More Samsung interview questions
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
- Task Scheduler with CooldownGoDaddy · OA
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Return Exact Cash Register ChangeBank of America · OABank of America interview questions