Need help with Write an efficient algorithm function: A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. the small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
Write a function:
class Solution { public int solution(int X, int Y, int D); }
That, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.
For example, given:
X = 10
Y = 85
D = 30
The function should return 3, because the frog will be positioned as follows:
Write an efficient algorithm for the following assumptions:
* X,Y and D are integers within the range [1..1,000,000,000];
* X <_ Y.
Thank you for the help.
Just subtract X from Y, then divide it by D. If it's a decimal, round it up.
const froggy = (X, Y, D) => Math.ceil((Y - X) / D);
console.log(froggy(10, 85, 30));