PHP Program to Find the Size of Subarray With Maximum Sum Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an Array, the task is to find the size of the subarray that yields the maximum sum. This article provides a comprehensive guide on how to implement a PHP program to solve this problem efficiently.Examples: Input: Arr = [ 1, -2, 1, 1, -2, 1 ]Output: Length of the subarray is 2Explanation: The subarray with consecutive elements and maximum sum is [ 1, 1 ]. So the length is 2.Input: Arr = [ -2, -3, 4, -1, -2, 1, 5, -3 ]Output: Length of the subarray is 5Explanation: The subarray with consecutive elements and maximum sum is [ 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 <?php // Function to find the maximum contiguous // subarray and print its starting and end // index 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; } } return ($end - $start + 1); } // Driver program to test maxSubArraySum $a = [-2, -3, 4, -1, -2, 1, 5, -3]; $n = count($a); echo maxSubArraySum($a, $n); ?> Output5Time Complexity: O(N) where N is size of the input array. This is because a for loop is executing from 1 to size of the array.Space Complexity: O(1) as no extra space has been taken.Please refer complete article on Size of The Subarray With Maximum Sum for more details! Comment More infoAdvertise with us K kartik Follow Improve Article Tags : PHP subarray subarray-sum Similar Reads Maximum Subarray Sum - Kadane's Algorithm Given an integer array arr[], find the subarray (containing at least one element) which has the maximum possible sum, and return that sum.Note: A subarray is a continuous part of an array.Examples:Input: arr[] = [2, 3, -8, 7, -1, 2, 3]Output: 11Explanation: The subarray [7, -1, 2, 3] has the largest 8 min read PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read Subarrays, Subsequences, and Subsets in Array What is a Subarray?A subarray is a contiguous part of array, i.e., Subarray is an array that is inside another array. In general, for an array of size n, there are n*(n+1)/2 non-empty subarrays. For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are: (1), 10 min read PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param 8 min read Generating All Subarrays Given an array arr[], the task is to generate all the possible subarrays of the given array.Examples: Input: arr[] = [1, 2, 3]Output: [ [1], [1, 2], [2], [1, 2, 3], [2, 3], [3] ]Input: arr[] = [1, 2]Output: [ [1], [1, 2], [2] ]Iterative ApproachTo generate a subarray, we need a starting index from t 8 min read Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET 4 min read Subarray with Given Sum Given a 1-based indexing array arr[] of non-negative integers and an integer sum. You mainly need to return the left and right indexes(1-based indexing) of that subarray. In case of multiple subarrays, return the subarray indexes which come first on moving from left to right. If no such subarray exi 10 min read Like