0

I have below program in JAVA.

private static int frogJump(int[] arrEl,int postion) {
        /** Marker array for the leaf found on the way. */
        boolean[] leafArray = new boolean[postion+1];

        /** Total step needed for frog. */
        int steps = postion;

        for(int i = 0; i<arrEl.length; i++) {

            /** if leaf needed for frog and it does not exist earlier. **/
            if(postion>=arrEl[i] && !leafArray[arrEl[i]]) {

                /* Mark leaf found */
                leafArray[arrEl[i]] = true;

                /** Reduce the step by one(coz one jump found). */
                steps--;
            }

            if(steps == 0 && arrEl[i]==postion) {
                return i;
            }
        }

        return -1;
    }

Which i want to convert in PHP. Till now what i have done is

function solution ($A = [], $Position) {

    $StonesArray = array();

    $StonesArray[TRUE] = $Position + 1;

    $steps = $Position;

    for($i = 0; $i< count($A); $i++) {

        echo "<pre>";
        print_r($StonesArray);

        if($Position >= $A[$i] && !$StonesArray[$A[$i]]) {


            $StonesArray[$A[$i]] = true;


            $steps--;
        }

        if($steps == 0 && $A[$i] == $Position) {
            return $i;
        }
    }

    return -1;
}

$GetSolution  = solution([3,2,1], 1);
echo "<pre>";
print_r($GetSolution);

above program should return 3. but after i have converted the program to PHP language its not returning the expected value.

I am sure I have done everything correct except converting the below line

boolean[] leafArray = new boolean[postion+1];

How to write this line in PHP?

19
  • This is not C. I can't tell for sure what it is (C#? C++?) Please determine yourself and correct the tag. Commented Aug 18, 2017 at 7:42
  • Check the code on codesolution.org/frog-jump-through-leaves-problem Commented Aug 18, 2017 at 7:43
  • Looking into the source code says it highlights for java (It has the class syntaxhighlighter java) Commented Aug 18, 2017 at 7:45
  • I think that's C#, anyway I don't think you can create an array with predefined size in PHP. And usually you don't need to. Just use $leafArray = array(); Commented Aug 18, 2017 at 7:45
  • let me edit my Question then,, Commented Aug 18, 2017 at 7:46

1 Answer 1

1

I just translated your original Java code to PHP 1:1, most of it can be used as is with little change, look at this example:

function frogJump(array $arrEl, $postion) {
    /** Marker array for the leaf found on the way. */
    $leafArray = array_fill(0, $postion+1, false);

    /** Total step needed for frog. */
    $steps = $postion;

    for($i = 0; $i<count($arrEl); $i++) {

        /** if leaf needed for frog and it does not exist earlier. **/
        if($postion>=$arrEl[$i] && !$leafArray[$arrEl[$i]]) {

            /* Mark leaf found */
            $leafArray[$arrEl[$i]] = true;

            /** Reduce the step by one(coz one jump found). */
            $steps--;
        }

        if($steps == 0 && $arrEl[$i]==$postion) {
            return $i;
        }
    }

    return -1;
}

print_r(frogJump([3,2,1], 1)); outputs 2.

I also compiled the Java code and the output is also 2, so it seems correct to me? Run with System.out.println(frogJump(new int[]{3,2,1}, 1));

Sign up to request clarification or add additional context in comments.

2 Comments

I have not enough rep. to access chat.
I think if my answer solves your problem you can just accept it and the question is closed. If you have further questions just ask here in the comments or create a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.