0

I am a beginner programmer and am trying out the Codility frog jump question. Here is my code solution:

int solution(int, int, int, unsigned long int&);

int main(){

    unsigned long int stepsTaken = 1;

    int x = 10;
    int y = 85;
    int d = 30;

    solution(x, y, d, stepsTaken);

    cout << "Total Steps Taken: " << stepsTaken << endl;
}

int solution( int X, int Y, int D, unsigned long int &stepsTaken) {

    int currentPosition = X;
    int positionToGetTo = Y;
    int stepsJumpedEachTime = D;

    currentPosition += stepsJumpedEachTime;

    if(currentPosition < positionToGetTo){

        stepsTaken++;
        solution(currentPosition, positionToGetTo, stepsJumpedEachTime, stepsTaken);
    }

    return stepsTaken;
}

Now the problem I am having is when I attempt to fullfil the requirements to deal with a number range from 1-1000000000. If I change int y above to say 2000000 I get a negative returned. unsigned long int should return a positive number but when I use 2000000 it returns negative.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2795662
  • 33
  • 1
  • 5
  • 3
    For large Y, this is in danger of causing a stack overflow. – xen-0 Sep 19 '13 at 15:28
  • Hmm. (as expected looking at the code) I do not get a negative number with 2000000 as y. Lowering d causes a stack overflow as the above comment mentioned.. http://ideone.com/jErmdG – drescherjm Sep 19 '13 at 15:50
  • 2
    I will just say, recursion and iteration are not necessary to solve the problem at hand. – xen-0 Sep 19 '13 at 15:53

8 Answers8

2

This happens because int numbers in C++ have limits - see http://www.cplusplus.com/reference/climits/. In most cases, standard types should satisfy your needs.

If no standard type is big enough, see What's the best (for speed) arbitrary-precision library for C++?

Here is a simpler version of your code, what do you think?

unsigned long int solution( int currentPosition, int positionToGetTo , int stepsJumpedEachTime) {

    if (currentPosition >= positionToGetTo)
        return 0; 

    return 1 + solution(currentPosition + stepsJumpedEachTime, positionToGetTo, stepsJumpedEachTime);

}

int main(){

    int x = 10;
    int y = 85;
    int d = 30;

    unsigned long int stepsTaken = solution(x, y, d);

    cout << "Total Steps Taken: " << stepsTaken << endl;
}
Community
  • 1
  • 1
Yulia V
  • 3,507
  • 10
  • 31
  • 64
  • Shouldn't the positions also be unsigned long int if we allow the steps to get to a position be unsigned long int? – drescherjm Sep 19 '13 at 15:33
  • thanks for the reply - tried this method and also tried setting the 3 variables - currentPosition, positionToGetTo and stepsJumpedEachTime to the datatype unsigned long int - but i'm getting a stack overflow. Maybe recursion isn't the most suitable way to solve this. – user2795662 Sep 19 '13 at 18:44
  • 1
    @user2795662 Think of this as a math problem. Solve it on paper. – drescherjm Sep 20 '13 at 13:01
  • recursion is almost never optimal and almost always avoidable. – Yulia V Sep 20 '13 at 16:12
2

Iteration takes too long.

int destination = Y - X;
int steps = destination / D;
int remainder = destination % D;
if (remainder != 0){
    steps++;
}
return steps;
1

Got this to work without using recursion as follows:

#include <iostream>
#include <iostream>

using namespace std;

unsigned long int solution( int currentPosition,int positionToGetTo ,int       stepsJumpedEachTime);

int main(){

int x = 10;
int y = 1000000000;
int d = 30;

unsigned long int stepsTaken = solution(x, y, d);

cout << "Total Steps Taken: " << stepsTaken << endl;
}

unsigned long int solution(int currentPosition, int positionToGetTo ,int   stepsJumpedEachTime){

unsigned long int stepsTaken = 1;

currentPosition += stepsJumpedEachTime;

while (currentPosition < positionToGetTo){

    currentPosition += stepsJumpedEachTime;
    stepsTaken++;

}

return stepsTaken;

}
user2795662
  • 33
  • 1
  • 5
0

I hope this will work:

class Solution {
    public int solution(int X, int Y, int D) {
    if(X>=Y)
    return 0;
    if((Y-X)%D!=0) return (Y-X)/D+1;
    else
    return (Y-X)/D;
   }
}
user1981275
  • 13,002
  • 8
  • 72
  • 101
0

A 100% "float" version which is about three times faster on my x64 CPU than the "int" version:

#include <cmath>

int solution(int X, int Y, int D)
{
    return std::floor( (double)( Y - 1 - X ) / D + 1 );
}
0

C solution. 100 out of 100 points on Codility

int FrogJmp(int X, int Y, int D) {

    int result = 0;
    int dest = Y - X;
    result = dest / D;

    if (dest % D != 0) {
        result++;
    }
    return result;
}
0

Solution using ceil:

#include <cmath>
#include <climits>

int solution(int X, int Y, int D) 
{
    if (X >= Y)
        return 0;

    if (D == 0)
        return INT_MAX;

    return std::ceil((double)(Y - X) / D);
}
agnor
  • 259
  • 1
  • 3
  • 12
0

100% score - C code :Codility: FrogJmp

int solution(int X, int Y, int D) {
    // write your code in C90
    if (Y == X) 
      return 0;
    else
      return (((Y - X) / D) + (((Y - X) % D) > 0 ? 1 : 0));     
}

expected worst-case time complexity is O(1) means no iteration...

Arif Basri
  • 11
  • 4