My experience on my daily works... helping others ease each other

Saturday, January 11, 2020

Codility - FrogJmp (Count minimal number of jumps from position X to Y)

Codility - FrogJmp

Count the minimal number of jumps from position X to Y

FrogJmp is the third lesson number 1 out of three in the list for Time Complexity algorithm. Basically, it is an algorithm to count the number of the jump from one X location to Y location when the number of the step taken is given as Z for each single jump

public int solution(int X, int Y, int D){
        int a = 0;
        Y = Y - X; //setting the initial value test

        if (Y >= 1){ //evaluating the Y value
            a = Y/D; //setting the initial return value
            if (Y % D > 0){ //evaluating the possible number of jump to add additional value
                a++;
            }
        }
        if ( a == 0 && (Y%D==0) && Y > X ){ //final check
            a = 1;
        }
        return a;
    };

Result of the code

Code is downloadable from
Github - https://github.com/masteramuk/codility-lessoncode.git
Share:

0 comments:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates