Created
June 21, 2016 05:19
-
-
Save ssi-anik/190e57dd29b44e54364192652fd52a33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getSum($first, $second){ | |
| // reverse the inputs | |
| $reversed_first = strrev($first); | |
| $reversed_second = strrev($second); | |
| // get the max string length | |
| $length = strlen($reversed_first) > strlen($reversed_second) ? strlen($reversed_first) : strlen($reversed_second); | |
| // carry will be zero initially | |
| $carry = 0; | |
| // sum array for storing the | |
| $sum = []; | |
| for($i = 0; $i < $length; ++$i){ | |
| $current_sum = (isset($reversed_first[$i]) ? $reversed_first[$i] : 0) + (isset($reversed_second[$i]) ? $reversed_second[$i] : 0) + $carry; | |
| $carry = (int) $current_sum/10; | |
| $sum[] = $current_sum%10; | |
| } | |
| return strrev(implode("", $sum)); | |
| } | |
| $handle = fopen ("php://stdin","r"); | |
| fscanf($handle,"%d",$n); | |
| $arr_temp = fgets($handle); | |
| $arr = explode(" ",$arr_temp); | |
| $sum = $arr[0]; | |
| for($i = 1; $i < count($arr); ++$i){ | |
| $sum = getSum($sum, $arr[$i]); | |
| } | |
| echo $sum; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment