Skip to content

Commit

Permalink
Merge pull request #1068 from Kiran-Jadhav200/kiran
Browse files Browse the repository at this point in the history
issue no: #883 completed Maximum subarray
  • Loading branch information
pankaj-bind authored Oct 20, 2024
2 parents 47a0015 + d29852f commit db9bbc8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 1D Arrays/Maximum Circular Sum Subarray/Program.c
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;
}
26 changes: 26 additions & 0 deletions 1D Arrays/Maximum Circular Sum Subarray/README.md
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.
Binary file added 1D Arrays/Maximum Circular Sum Subarray/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit db9bbc8

Please sign in to comment.