-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMaximum Sum Circular Subarray.cpp
76 lines (54 loc) · 2.17 KB
/
Maximum Sum Circular Subarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <algorithm>
using namespace std;
/*
In this solution we are have to give maximum sum subarray circular. So what we do here is we take normal karden algorithms for normal maximum sum subarray. Then we negate the array and take the maximum sum. Maximum negative mean minimum positive. So we subtract the minimum positive from the total sum to get the maximum total. Then we compare the both whichever is greater or smaller.
*/
// Function to find contiguous sub-array with the largest sum
// in given set of integers
int kadane(int arr[], int n)
{
// stores maximum sum sub-array found so far
int max_so_far = 0;
// stores maximum sum of sub-array ending at current position
int max_ending_here = 0;
// traverse the given array
for (int i = 0; i < n; i++)
{
// update maximum sum of sub-array "ending" at index i (by adding
// current element to maximum sum ending at previous index i-1)
max_ending_here = max_ending_here + arr[i];
// if maximum sum is negative, set it to 0 (which represents
// an empty sub-array)
max_ending_here = max(max_ending_here, 0);
// update result if current sub-array sum is found to be greater
max_so_far = max(max_so_far, max_ending_here);
}
return max_so_far;
}
// Function to find maximum sum circular subarray in a given array
int KadaneCircular(int arr[], int n)
{
// negate all elements of the array
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
// run Kadane's algorithm on modified array
int negMaxSum = kadane(arr, n);
// restore the array
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
/* return maximum of
1. sum returned by Kadane's algorithm on original array.
2. sum returned by Kadane's algorithm on modified array +
sum of all elements of the array.
*/
return max(kadane(arr, n), accumulate(arr, arr + n, 0) + negMaxSum);
}
int main()
{
int arr[] = { 2, 1, -5, 4, -3, 1, -3, 4, -1 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << "The sum of subarray with the largest sum is " <<
KadaneCircular(arr, n);
return 0;
}