Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Maximum Circular Subarray Sum #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Program to calculate Maximum Circular Subarray Sum

#include<bits/stdc++.h>
using namespace std;

// Kadane Algorithm to find the Maximum Sum of continuous elements in an array
int kadaneMax(int arr[], int n) {
int ans = arr[0], prevBest = arr[0];
for(int i=1; i<n; i++) {
prevBest = max(prevBest + arr[i], arr[i]);
ans = max(ans, prevBest);
}
return ans;
}


// Kadane Algorithm to find the Minimum Sum of continuous elements in an array
int kadaneMin(int arr[], int n) {
int ans = arr[0], prevBest = arr[0];
for(int i=1; i<n; i++) {
prevBest = min(prevBest + arr[i], arr[i]);
ans = min(ans, prevBest);
}
return ans;
}

// Function to calculate Maximum sum
int MaxCircularSubarraySum(int arr[], int n) {

// Count the number of negative elements in array
int count = 0;

// Store the total sum of all the elements of array
int TotalSum = 0;

for(int i=0; i<n; i++) {
if(arr[i] < 0) {
count++;
}
TotalSum = TotalSum + arr[i];
}

// Here we are using inverted sum trick
// Maximum sum of circular array can be Max of
// case 1: maximum sum of acyclic array
// case 2: total - minimum sum of acyclic array

if(count == n) { // Edge case if all elements are negative
return kadaneMax(arr, n);
}
else {
return max(kadaneMax(arr, n), TotalSum - kadaneMin(arr, n));
}
}

int main() {
int n; // size of array
cin >> n;
int arr[n];
for(int i=0; i<n; i++) {
cin >> arr[i];
}
cout << MaxCircularSubarraySum(arr, n); // print maxSum
return 0;
}