-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1068 from Kiran-Jadhav200/kiran
issue no: #883 completed Maximum subarray
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// C program to find maximum Subarray Sum in Circular | ||
// subarray by considering all possible subarrays | ||
|
||
#include <stdio.h> | ||
|
||
int circularSubarraySum(int arr[], int n) { | ||
int totalSum = 0; | ||
int currMaxSum = 0, currMinSum = 0; | ||
int maxSum = arr[0], minSum = arr[0]; | ||
|
||
for(int i = 0; i < n; i++) { | ||
|
||
// Kadane's to find maximum sum subarray | ||
currMaxSum = (currMaxSum + arr[i] > arr[i]) ? | ||
currMaxSum + arr[i] : arr[i]; | ||
maxSum = (maxSum > currMaxSum) ? maxSum : currMaxSum; | ||
|
||
// Kadane's to find minimum sum subarray | ||
currMinSum = (currMinSum + arr[i] < arr[i]) ? | ||
currMinSum + arr[i] : arr[i]; | ||
minSum = (minSum < currMinSum) ? minSum : currMinSum; | ||
|
||
// Sum of all the elements of input array | ||
totalSum += arr[i]; | ||
} | ||
|
||
int normalSum = maxSum; | ||
int circularSum = totalSum - minSum; | ||
|
||
// If the minimum subarray is equal to total Sum | ||
// then we just need to return normalSum | ||
if(minSum == totalSum) | ||
return normalSum; | ||
|
||
return (normalSum > circularSum) ? normalSum : circularSum; | ||
} | ||
|
||
int main() { | ||
int arr[] = {8, -8, 9, -9, 10, -11, 12}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
printf("%d\n", circularSubarraySum(arr, n)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Maximum Circular Sum Subarray | ||
|
||
## Problem Statement | ||
|
||
Given an array of integers, find the maximum sum of a subarray with the constraint that the subarray may be circular. In other words, the subarray can wrap around to the beginning or end of the array. | ||
|
||
## Algorithm Approach | ||
|
||
To solve this problem, we can use the Kadane's algorithm, which is a well-known algorithm for finding the maximum subarray sum in a linear time complexity. | ||
|
||
1. Initialize two variables, `max_sum` and `min_sum`, to the first element of the array. | ||
2. Initialize two more variables, `current_max` and `current_min`, to the first element of the array. | ||
3. Iterate through the array starting from the second element. | ||
4. Update `current_max` and `current_min` as follows: | ||
- `current_max = max(current_max + element, element)` | ||
- `current_min = min(current_min + element, element)` | ||
5. Update `max_sum` and `min_sum` as follows: | ||
- `max_sum = max(max_sum, current_max)` | ||
- `min_sum = min(min_sum, current_min)` | ||
6. If `max_sum` is negative, it means that all elements in the array are negative. In this case, return `max_sum` as the maximum circular sum. | ||
7. Otherwise, return the maximum of `max_sum` and the sum of all elements in the array minus `min_sum`. | ||
|
||
![alt text](image.png) | ||
## Time Complexity | ||
|
||
The time complexity of this algorithm is O(n), where n is the size of the input array. This is because we iterate through the array only once. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.