Grid based problem: Walmart Interview Question - Doubt

Assume a Infinite 2-D grid, you always start from the cell (0, 0). You have to reach (X, Y) from (0, 0).
You can move from any cell (a, b) to any cell (c, d) if the following conditions are met:

1. c and d are integers.
2. |a - c| + |b - d| = K

Here, the value of K is given in the input.

Find if it is the possible to reach the destination (X, Y). If possible, what is the minimum number of moves you can take to reach the final destination?

Constraints:

-10^5 <= X, Y <= 10^5
1 <= K <= 10^9

Example:

Input:
5
1 3
Output:
2

I'm unable to solve this particular question. If anyone can able to solve it, please help me to understand the solution.

Comments (0)