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 is the PHP code of this solution. Teached me a lot. Thanks.
<?php$_fp=fopen("php://stdin","r");/* Enter your code here. Read input from STDIN. Print output to STDOUT */fscanf($_fp,"%d %d",$size,$operations);$array=[];$max=PHP_INT_MIN;for($i=0;$i<$operations;$i++){fscanf($_fp,"%d %d %d",$start,$end,$value);$negativeValue=$value*-1;if(!isset($array[$start]))$array[$start]=$value;else$array[$start]+=$value;if($end+1<=$size){if(!isset($array[$end+1]))$array[$end+1]=$negativeValue;else$array[$end+1]-=$value;}}ksort($array);$sum=0;foreach($arrayas$amount){$sum+=$amount;if($sum>$max)$max=$sum;}echo$max;?>
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Here is the PHP code of this solution. Teached me a lot. Thanks.