PHP Program for Maximum Circular Subarray Sum
Last Updated :
22 Jul, 2024
Given n numbers (both +ve and -ve), arranged in a circle, find the maximum sum of consecutive numbers.
Examples:
Input: Arr = [ 8, -8, 9, -9, 10, -11, 12 ]
Output: 22 (12 + 8 - 8 + 9 - 9 + 10)
Input: Arr = [ 10, -3, -4, 7, 6, 5, -4, -1 ]
Output: 23 (7 + 6 + 5 - 4 -1 + 10)
Input: Arr = [ -1, 40, -14, 7, 6, 5, -4, -1 ]
Output: 52 (7 + 6 + 5 - 4 - 1 - 1 + 40)
Approach: There can be two cases for the maximum sum:
- Case 1: The elements that contribute to the maximum sum are arranged such that no wrapping is there. Examples: {-10, 2, -1, 5}, {-2, 4, -1, 4, -1}. In this case, Kadane's algorithm will produce the result.
- Case 2: The elements contribute to the maximum sum are arranged such that wrapping is there. Examples: {10, -12, 11}, {12, -5, 4, -8, 11}. In this case, we change wrapping to non-wrapping. Let us see how. Wrapping of contributing elements implies non-wrapping of non-contributing elements, so find out the sum of non-contributing elements and subtract this sum from the total sum. To find out the sum of non-contributions, invert the sign of each element and then run Kadane's algorithm.
Our array is like a ring and we have to eliminate the maximum continuous negative that implies maximum continuous positive in the inverted arrays. Finally, we compare the sum obtained in both cases and return the maximum of the two sums.
The following are implementations of the above method.
PHP
<?php
// PHP program for maximum
// contiguous circular sum problem
// The function returns maximum
// circular contiguous sum $a[]
function maxCircularSum($a, $n) {
// Case 1: get the maximum sum
// using standard kadane' s algorithm
$max_kadane = kadane($a, $n);
// Case 2: Now find the maximum
// sum that includes corner elements.
$max_wrap = 0;
for ($i = 0; $i < $n; $i++) {
$max_wrap += $a[$i]; // Calculate array-sum
$a[$i] = -$a[$i]; // invert the array (change sign)
}
// max sum with corner elements will be:
// array-sum - (-max subarray sum of inverted array)
$max_wrap = $max_wrap + kadane($a, $n);
// The maximum circular sum will be maximum of two sums
return $max_wrap > $max_kadane ? $max_wrap : $max_kadane;
}
// Standard Kadane's algorithm to
// find maximum subarray sum
function kadane($a, $n) {
$max_so_far = 0;
$max_ending_here = 0;
for ($i = 0; $i < $n; $i++) {
$max_ending_here = $max_ending_here + $a[$i];
if ($max_ending_here < 0) {
$max_ending_here = 0;
}
if ($max_so_far < $max_ending_here) {
$max_so_far = $max_ending_here;
}
}
return $max_so_far;
}
/* Driver code */
$a = [11, 10, -20, 5, -3, -5, 8, -13, 10];
$n = count($a);
echo "Maximum circular sum is " . maxCircularSum($a, $n);
?>
OutputMaximum circular sum is 31
Complexity Analysis
- Time Complexity: O(n), where n is the number of elements in the input array. As only linear traversal of the array is needed.
- Auxiliary Space: O(1). As no extra space is required.
Note that the above algorithm doesn't work if all numbers are negative, e.g., {-1, -2, -3}. It returns 0 in this case. This case can be handled by adding a pre-check to see if all the numbers are negative before running the above algorithm.
Please refer complete article on Maximum circular subarray sum for more details!
Similar Reads
Javascript Program for Maximum circular subarray sum Given n numbers (both +ve and -ve), arranged in a circle, find the maximum sum of consecutive numbers. Examples: Input: a[] = {8, -8, 9, -9, 10, -11, 12}Output: 22 (12 + 8 - 8 + 9 - 9 + 10)Input: a[] = {10, -3, -4, 7, 6, 5, -4, -1} Output: 23 (7 + 6 + 5 - 4 -1 + 10) Input: a[] = {-1, 40, -14, 7, 6,
5 min read
Maximum Circular Subarray Sum Given a circular array arr[] of size n, find the maximum possible sum of a non-empty subarray.Examples: Input: arr[] = {8, -8, 9, -9, 10, -11, 12}Output: 22Explanation: Circular Subarray {12, 8, -8, 9, -9, 10} has the maximum sum, which is 22.Input: arr[] = {10, -3, -4, 7, 6, 5, -4, -1}Output: 23 Ex
15+ min read
Maximum circular subarray sum of size K Given an array arr of size N and an integer K, the task is to find the maximum sum subarray of size k among all contiguous sub-array (considering circular subarray also). Examples: Input: arr = {18, 4, 3, 4, 5, 6, 7, 8, 2, 10}, k = 3 Output: max circular sum = 32 start index = 9 end index = 1 Explan
6 min read
Maximum sum subarray of size range [L, R] Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive). Example: Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3 Output: 5 Explanation: Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for
8 min read
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Print subarray with maximum sum Given an array arr[], the task is to print the subarray having maximum sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}Output: {6, -2, -3, 1, 5}Explanation: The subarray {6, -2, -3
13 min read
Sum of maximum of all subarrays | Divide and Conquer Given an array arr[] of length N, the task is to find the sum of the maximum elements of every possible sub-array of the array.Examples: Input : arr[] = {1, 3, 1, 7} Output : 42 Max of all sub-arrays: {1} - 1 {1, 3} - 3 {1, 3, 1} - 3 {1, 3, 1, 7} - 7 {3} - 3 {3, 1} - 3 {3, 1, 7} - 7 {1} - 1 {1, 7} -
12 min read
Longest subarray having maximum sum Given an array arr[] containing n integers. The problem is to find the length of the subarray having maximum sum. If there exists two or more subarrays with maximum sum then print the length of the longest subarray.Examples: Input : arr[] = {5, -2, -1, 3, -4}Output : 4There are two subarrays with ma
12 min read
Maximum Subarray Sum - Kadane's Algorithm Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -4}Output: -2Explanation: The subarray {-2} has the largest s
9 min read
Find Maximum Sum Strictly Increasing Subarray Given an array of positive integers. Find the maximum sum of strictly increasing subarrays. Note that this problem is different from maximum subarray sum and maximum sum increasing subsequence problems. Examples: Input : arr[] = {1, 2, 3, 2, 5, 1, 7}Output : 8Explanation : Some Strictly increasing s
7 min read