A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Maximum Subarray in Php page! Here, you'll find the source code for this program as well as a description of how the program works.
<?php
function usage()
{
exit('Usage: Please provide a list of integers in the format: "1, 2, 3, 4, 5"');
}
function parse_int($str_value)
{
// Remove leading and trailing spaces
$str_value = trim($str_value);
// Make sure all digits
if (preg_match("/^[+-]?\d+$/", $str_value) === FALSE)
{
return FALSE;
}
// Make sure valid integer
if (
filter_var(
$str_value,
FILTER_VALIDATE_INT,
array(
'options' => array(
'decimal' => TRUE,
'min_range' => PHP_INT_MIN,
'max_range' => PHP_INT_MAX
)
)
) === FALSE
)
{
return FALSE;
}
return intval($str_value);
}
function parse_int_array($str_values)
{
$str_array = explode(",", $str_values);
$values = array();
foreach ($str_array as $str_value)
{
$value = parse_int($str_value);
if ($value === FALSE)
{
return FALSE;
}
array_push($values, $value);
}
return $values;
}
// Find maximum subarray using Kadane's algorithm.
// Source: https://en.wikipedia.org/wiki/Maximum_subarray_problem#No_empty_subarrays_admitted
function maximum_subarray($values)
{
$best_sum = PHP_INT_MIN;
$current_sum = 0;
foreach ($values as $value)
{
$current_sum = $value + max(0, $current_sum);
$best_sum = max($best_sum, $current_sum);
}
return $best_sum;
}
// Exit if too few arguments
if (count($argv) < 2)
{
usage();
}
// Parse 1st command-line argument. Exit if invalid
$values = parse_int_array($argv[1]);
if ($values === FALSE)
{
usage();
}
// Calculate maximum subarray and display
$max_value = maximum_subarray($values);
echo "${max_value}\n";
Maximum Subarray in Php was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.