Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Program for Largest Sum Contiguous Subarray
The Largest Sum Contiguous Subarray problem, also known as the Maximum Subarray Problem, involves finding a contiguous subarray within a given array that has the largest sum. This is a classic algorithmic problem that can be efficiently solved using Kadane's Algorithm.
Using Kadane's Algorithm
Kadane's Algorithm efficiently finds the maximum sum contiguous subarray in O(n) time. It works by maintaining two variables
max_so_far: Maximum sum found so far
max_ending_here: Maximum sum ending at current position
<?php
// PHP program to find largest contiguous subarray sum
function maxSubArraySum($a, $size)
{
$max_so_far = PHP_INT_MIN;
$max_ending_here = 0;
for ($i = 0; $i < $size; $i++)
{
$max_ending_here = $max_ending_here + $a[$i];
if ($max_so_far < $max_ending_here)
$max_so_far = $max_ending_here;
if ($max_ending_here < 0)
$max_ending_here = 0;
}
return $max_so_far;
}
// Driver code
$a = array(-2, 1, -3, 4, -1, 2, 1, -5, 4);
$n = count($a);
$max_sum = maxSubArraySum($a, $n);
echo "Maximum contiguous sum is " . $max_sum;
?>
Maximum contiguous sum is 6
Using Dynamic Programming Approach
This approach maintains the maximum sum at each position by deciding whether to extend the previous subarray or start a new one ?
<?php
function maxSubArraySum($a, $size)
{
$max_so_far = $a[0];
$curr_max = $a[0];
for ($i = 1; $i < $size; $i++)
{
$curr_max = max($a[$i], $curr_max + $a[$i]);
$max_so_far = max($max_so_far, $curr_max);
}
return $max_so_far;
}
// Driver Code
$a = array(-2, 1, -3, 4, -1, 2, 1, -5, 4);
$n = sizeof($a);
$max_sum = maxSubArraySum($a, $n);
echo "Maximum contiguous sum is " . $max_sum;
?>
Maximum contiguous sum is 6
Finding Subarray Indices
This enhanced version also returns the starting and ending indices of the maximum subarray ?
<?php
function maxSubArraySum($a, $size)
{
$max_so_far = PHP_INT_MIN;
$max_ending_here = 0;
$start = 0;
$end = 0;
$s = 0;
for ($i = 0; $i < $size; $i++)
{
$max_ending_here += $a[$i];
if ($max_so_far < $max_ending_here)
{
$max_so_far = $max_ending_here;
$start = $s;
$end = $i;
}
if ($max_ending_here < 0)
{
$max_ending_here = 0;
$s = $i + 1;
}
}
echo "Maximum contiguous sum is " . $max_so_far . "<br>";
echo "Starting index " . $start . "<br>";
echo "Ending index " . $end . "<br>";
}
// Driver Code
$a = array(-2, 1, -3, 4, -1, 2, 1, -5, 4);
$n = sizeof($a);
maxSubArraySum($a, $n);
?>
Maximum contiguous sum is 6 Starting index 3 Ending index 6
Comparison
| Approach | Time Complexity | Space Complexity | Returns Indices |
|---|---|---|---|
| Kadane's Algorithm | O(n) | O(1) | No |
| Dynamic Programming | O(n) | O(1) | No |
| Enhanced Kadane's | O(n) | O(1) | Yes |
Conclusion
Kadane's Algorithm provides an efficient O(n) solution for finding the maximum sum contiguous subarray. The dynamic programming approach offers a cleaner implementation, while the enhanced version can also track subarray indices for practical applications.
