# Php Program for Size of The Subarray With Maximum Sum

• Last Updated : 20 Jan, 2022

An array is given, find length of the subarray having maximum sum.

Examples :

```Input :  a[] = {1, -2, 1, 1, -2, 1}
Output : Length of the subarray is 2
Explanation: Subarray with consecutive elements
and maximum sum will be {1, 1}. So length is 2

Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }
Output : Length of the subarray is 5
Explanation: Subarray with consecutive elements
and maximum sum will be {4, -1, -2, 1, 5}. ```

This problem is mainly a variation of Largest Sum Contiguous Subarray Problem.
The idea is to update starting index whenever sum ending here becomes less than 0.

## PHP

 `= 0) ` `    ``{ ` `        ``\$y``++; ` `        ``\$slope_error_new` `-= 2 * (``\$x2` `- ``\$x1``); ` `    ``} ` `} ` `} ` ` `  `// Driver Code ` `\$x1` `= 3; ``\$y1` `= 2; ``\$x2` `= 15; ``\$y2` `= 5; ` `bresenham(``\$x1``, ``\$y1``, ``\$x2``, ``\$y2``); ` ` `  `// This code is contributed by nitin mittal. ` `?> `

Output :

`5`

Please refer complete article on Size of The Subarray With Maximum Sum for more details!

My Personal Notes arrow_drop_up
Related Articles