Java Program for Maximum circular subarray sum
Last Updated :
29 Dec, 2021
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, -4, -1}
Output: 52 (7 + 6 + 5 - 4 - 1 - 1 + 40)
Method 1 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 which 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.
Java
// Java program for maximum contiguous circular sum problem
import java.io.*;
import java.util.*;
class Solution{
public static int kadane(int a[],int n){
int res = 0;
int x = a[0];
for(int i = 0; i < n; i++){
res = Math.max(a[i],res+a[i]);
x= Math.max(x,res);
}
return x;
}
//lets write a function for calculating max sum in circular manner as discuss above
public static int reverseKadane(int a[],int n){
int total = 0;
//taking the total sum of the array elements
for(int i = 0; i< n; i++){
total +=a[i];
}
// inverting the array
for(int i = 0; i<n ; i++){
a[i] = -a[i];
}
// finding min sum subarray
int k = kadane(a,n);
// max circular sum
int ress = total+k;
// to handle the case in which all elements are negative
if(total == -k ){
return total;
}
else{
return ress;
}
}
public static void main(String[] args)
{ int a[] = {1,4,6,4,-3,8,-1};
int n = 7;
if(n==1){
System.out.println("Maximum circular sum is " +a[0]);
}
else{
System.out.println("Maximum circular sum is " +Integer.max(kadane(a,n), reverseKadane(a,n)));
}
}
} /* This code is contributed by Mohit Kumar*/
Output:
Maximum 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.
Method 2
Approach: In this method, modify Kadane's algorithm to find a minimum contiguous subarray sum and the maximum contiguous subarray sum, then check for the maximum value between the max_value and the value left after subtracting min_value from the total sum.
Algorithm
- We will calculate the total sum of the given array.
- We will declare the variable curr_max, max_so_far, curr_min, min_so_far as the first value of the array.
- Now we will use Kadane's Algorithm to find the maximum subarray sum and minimum subarray sum.
- Check for all the values in the array:-
- If min_so_far is equaled to sum, i.e. all values are negative, then we return max_so_far.
- Else, we will calculate the maximum value of max_so_far and (sum - min_so_far) and return it.
The implementation of the above method is given below.
Output:
Maximum 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.
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[], find the maximum sum of any non-empty subarray. A circular array allows wrapping from the end back to the beginning.Note: A subarray can span from the end of the array and continue at the beginning.Examples: Input: arr[] = [8, -8, 9, -9, 10, -11, 12]Output: 22Explanatio
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
Java 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. Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. Kadane's Algorithm: Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for
5 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
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