Tasks Details
easy
1.
FrogJmp
Count minimal number of jumps from position X to Y.
Task Score
100%
Correctness
100%
Performance
100%
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 = 30the function should return 3, because the frog will be positioned as follows:
- after the first jump, at position 10 + 30 = 40
- after the second jump, at position 10 + 30 + 30 = 70
- after the third jump, at position 10 + 30 + 30 + 30 = 100
Write an efficient algorithm for the following assumptions:
- X, Y and D are integers within the range [1..1,000,000,000];
- X ≤ Y.
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 13 minutes
Notes
not defined yet
Task timeline
Code: 02:47:57 UTC,
java,
autosave
Code: 02:48:36 UTC,
java,
autosave
Code: 02:48:47 UTC,
java,
autosave
Code: 02:49:00 UTC,
java,
autosave
Code: 02:49:17 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = (Y-X)/D;
}
}
Code: 02:49:54 UTC,
java,
autosave
Code: 02:50:09 UTC,
java,
autosave
Code: 02:50:33 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = (Y-X)/D;
}
}
Code: 02:51:04 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
do res = (Y-X)/D;
}
}
Code: 02:51:15 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
}
}
Code: 02:51:28 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
return
}
}
Code: 02:52:17 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
sys
return
}
}
Code: 02:52:36 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.lang.math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
System.out.println(cal);
return 1;
}
}
Analysis
Compile error
Solution.java:2: error: cannot find symbol import java.lang.math; ^ symbol: class math location: package java.lang 1 error
Code: 02:52:57 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
System.out.println(cal);
return 1;
}
}
Code: 02:52:58 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (Y-X)/D;
System.out.println(cal);
return 1;
}
}
Analysis
expand all
Example tests
1.
0.012 s
WRONG ANSWER,
got 1 expected 3
stdout:
2.0
Code: 02:53:52 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (d(Y-X)/D;
System.out.println(cal);
return 1;
}
}
Code: 02:53:55 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 1 expected 3
stdout:
2.5
Code: 02:54:39 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
Code: 02:54:45 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
User test case 1:
{10, 10 , 5}
Analysis
expand all
Example tests
1.
0.012 s
WRONG ANSWER,
got 1 expected 3
stdout:
2.5
expand all
User tests
1.
0.001 s
RUNTIME ERROR,
invalid input, unexpected character: {
Code: 02:55:09 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
Code: 02:55:09 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
User test case 1:
[10, 10, 5]
Analysis
expand all
Example tests
1.
0.012 s
WRONG ANSWER,
got 1 expected 3
stdout:
2.5
Code: 02:56:15 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
System.out.println(cal);
return 1;
}
}
Code: 02:56:40 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 )
return 1;
}
}
Code: 02:56:57 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 ) res = cal
return 1;
}
}
Code: 02:57:09 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 ) res = cal;
return 1;
}
}
Code: 02:59:10 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 ) res = int a = (int) Math.round(doubleVar);
return 1;
}
}
Code: 02:59:21 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 ) res = (int) Math.round(doubleVar);
return 1;
}
}
Code: 02:59:44 UTC,
java,
autosave
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 )
res = (int) Math.floor(cal);
else
res = (int) Math.floor(cal);
return res;
}
}
Code: 02:59:51 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 )
res = (int) Math.floor(cal);
else
res = (int) Math.ceil(cal);
return res;
}
}
User test case 1:
[10, 10, 5]
Analysis
Code: 03:00:23 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 )
res = (int) Math.floor(cal);
else
res = (int) Math.ceil(cal);
return res;
}
}
User test case 1:
[10, 10, 5]
Analysis
Code: 03:00:28 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
import java.lang.Math;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = 0;
double cal = (double)(Y-X)/D;
double mod = (double)(Y-X)%D;
if( mod == 0 )
res = (int) Math.floor(cal);
else
res = (int) Math.ceil(cal);
return res;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
0.012 s
OK
2.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.012 s
OK
2.
0.008 s
OK
1.
0.008 s
OK