Skip to content

Instantly share code, notes, and snippets.

@hrubantjakub1
Created April 20, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrubantjakub1/294c132752a569613b789ef18794e01b to your computer and use it in GitHub Desktop.
Save hrubantjakub1/294c132752a569613b789ef18794e01b to your computer and use it in GitHub Desktop.
codility solution for frog jump in java
// Task is to find out how many jumps is needed to get from distance X to Y. Length of every jump is D.
it is one of codility tasks
class Solution {
// X = beginning, Y = finish, D = distance
public int solution(int X, int Y, int D) {
int state = X;
int count = 0;
while (state < Y) {
state = state + D;
count++;
}
return count;
}
public static void main(String[] args) {
int X = 10;
int Y = 90;
int D = 30;
}
}
@warren302
Copy link

Or you can do it without any loop, e.x.:
public int solution(int X, int Y, int D) {
return (Y-X) % D == 0? (Y-X) / D : (Y-X) /D +1;
}
if the distance (Y-X) is divisible by the jump (D) the frog can make it in distance/jump moves otherwise it has to make one more jump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment