Convert JAVA program to PHP code -


i have below program in java.

private static int frogjump(int[] arrel,int postion) {         /** marker array leaf found on way. */         boolean[] leafarray = new boolean[postion+1];          /** total step needed frog. */         int steps = postion;          for(int = 0; i<arrel.length; i++) {              /** if leaf needed frog , not exist earlier. **/             if(postion>=arrel[i] && !leafarray[arrel[i]]) {                  /* mark leaf found */                 leafarray[arrel[i]] = true;                  /** reduce step one(coz 1 jump found). */                 steps--;             }              if(steps == 0 && arrel[i]==postion) {                 return i;             }         }          return -1;     } 

which want convert in php. till have done

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. after have converted program php language not returning expected value.

i sure have done correct except converting below line

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

how write line in php?

i translated original java code php 1:1, of can used little change, @ example:

function frogjump(array $arrel, $postion) {     /** marker array leaf found on way. */     $leafarray = array_fill(0, $postion+1, false);      /** total step needed frog. */     $steps = $postion;      for($i = 0; $i<count($arrel); $i++) {          /** if leaf needed frog , not exist earlier. **/         if($postion>=$arrel[$i] && !$leafarray[$arrel[$i]]) {              /* mark leaf found */             $leafarray[$arrel[$i]] = true;              /** reduce step one(coz 1 jump found). */             $steps--;         }          if($steps == 0 && $arrel[$i]==$postion) {             return $i;         }     }      return -1; } 

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

i compiled java code , output 2, seems correct me? run system.out.println(frogjump(new int[]{3,2,1}, 1));


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -