Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = <<<END
- 31
- -3 4 23 6 -5 6 -124 -2 4 0 16 -3 0 -6 15 9 -3 -4 -1 6 0 -3 2 -2 2 -2 -2 2 2 2 -2
- END;
- //scrap the first line and turn the second line into an array
- $input = explode("\n", $input);
- array_shift($input);
- $input = explode(" ", $input[0]);
- //this is just going to have to be a brute force, sorry guys
- //one loop for length
- //another loop for position start
- $max_sum = array(null, null, null); //sum, start, length
- for($x = 1; $x < count($input); $x++){
- //this loop is for length
- for($y = 0; $y < count($input)-$x; $y++){
- //this loop is for start position
- $sum = 0;
- for($z = $y; $z < $y+$x; $z++){
- //this loop is for accumulation
- $sum += $input[$z];
- }
- if($sum > $max_sum[0]){
- $max_sum[0] = $sum;
- $max_sum[1] = $y;
- $max_sum[2] = $x;
- }
- }
- }
- for($x = $max_sum[1]; $x < $max_sum[1]+$max_sum[2]; $x++){
- if($x == $max_sum[1]){
- echo $input[$x];
- }else{
- echo ' + '.$input[$x];
- }
- }
- echo ' = '.$max_sum[0];
Advertisement
Add Comment
Please, Sign In to add comment