We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my naive solution in PHP based on OPs logic.
<?php$handle=fopen("php://stdin","r");fscanf($handle,"%d %d %d",$n1,$n2,$n3);$h1_temp=fgets($handle);//Fetches line$h1=explode(" ",$h1_temp);//Separates on spacearray_walk($h1,'intval');//Forces each element to be an int$r1=array_reverse($h1);//Reverse to simulate stack$h1_commulative=0;//Variable to keep track of sums in the array$sumSteps1=array();//Array to hold sumsforeach($r1as$i){$sumSteps1[]=($h1_commulative+=$i);//Feed in possible stack heights}$steps1_rev=array_reverse($sumSteps1);//Reverse again so heights are stored from highest to lowest$h2_temp=fgets($handle);//Repeat for each array$h2=explode(" ",$h2_temp);array_walk($h2,'intval');$r2=array_reverse($h2);$h2_commulative=0;$sumSteps2=array();foreach($r2as$i){$sumSteps2[]=($h2_commulative+=$i);}$steps2_rev=array_reverse($sumSteps2);$h3_temp=fgets($handle);$h3=explode(" ",$h3_temp);array_walk($h3,'intval');$r3=array_reverse($h3);$h3_commulative=0;$sumSteps3=array();foreach($r3as$i){$sumSteps3[]=($h3_commulative+=$i);}$steps3_rev=array_reverse($sumSteps3);echogetMaxShared($steps1_rev,$steps2_rev,$steps3_rev);functiongetMaxShared($a,$b,$c){//Find the largest number shared between the three arraysforeach($aas$val){if(in_array($val,$b)&&in_array($val,$c)){return$val;}}return0;}?>
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Here's my naive solution in PHP based on OPs logic.