-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: add maximum circular subarray sum #2242
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6c0f738
Create maximum_circular_subarray.cpp
sumanbmondal 52552e3
Delete maximum_circular_subarray.cpp
sumanbmondal c1f1888
Create maximum_circular_subarray.cpp
sumanbmondal 6a2f1bc
Update maximum_circular_subarray.cpp
sumanbmondal a0a17ac
Merge branch 'master' into master
sumanbmondal e16f39b
updating DIRECTORY.md
4d8fb50
Update maximum_circular_subarray.cpp
sumanbmondal ed65ca0
Update maximum_circular_subarray.cpp
sumanbmondal 988b2ad
Update maximum_circular_subarray.cpp
sumanbmondal ba15ee6
Update maximum_circular_subarray.cpp
sumanbmondal c2cddfe
Update maximum_circular_subarray.cpp
sumanbmondal f00e9b2
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal b3a9ac4
Update maximum_circular_subarray.cpp
sumanbmondal 658fed4
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal 441d678
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal 696aa96
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal 78643d8
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal b26f54d
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal e16efd8
Merge branch 'master' into master
sumanbmondal ddf6da4
Update dynamic_programming/maximum_circular_subarray.cpp
sumanbmondal 3b8debe
Update maximum_circular_subarray.cpp
sumanbmondal d02d745
chore: apply suggestions from code review
Panquesito7 4398a81
Merge branch 'master' into master
sumanbmondal a8a2e48
Merge branch 'master' into master
sumanbmondal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,90 @@ | ||
/** | ||
* @file | ||
* @brief C++ program for maximum contiguous circular sum problem using [Kadane's Algorithm](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | ||
* @details | ||
* The idea is to 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. | ||
* For more information, check [Geeks For Geeks](https://www.geeksforgeeks.org/maximum-contiguous-circular-sum/) explanation page. | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for IO operations | ||
#include <vector> /// for std::vector | ||
|
||
|
||
/** | ||
* @namespace dynamic_programming | ||
* @brief Dynamic Programming algorithms | ||
*/ | ||
namespace dynamic_programming { | ||
/** | ||
* @brief returns the maximum contiguous circular sum of an array | ||
* | ||
* @param arr is the array/vector | ||
* @return int which is the maximum sum | ||
*/ | ||
int maxCircularSum(std::vector<int>& arr) | ||
{ | ||
// Edge Case | ||
if (arr.size() == 1) | ||
sumanbmondal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return arr[0]; | ||
|
||
// Sum variable which stores total sum of the array. | ||
int sum = 0; | ||
for (int i = 0; i < arr.size(); i++) { | ||
sum += arr[i]; | ||
} | ||
|
||
// Every variable stores first value of the array. | ||
int current_max = arr[0], max_so_far = arr[0], current_min = arr[0], min_so_far = arr[0]; | ||
|
||
// Concept of Kadane's Algorithm | ||
for (int i = 1; i < arr.size(); i++) { | ||
// Kadane's Algorithm to find Maximum subarray sum. | ||
current_max = std::max(current_max + arr[i], arr[i]); | ||
max_so_far = std::max(max_so_far, current_max); | ||
|
||
// Kadane's Algorithm to find Minimum subarray sum. | ||
current_min = std::min(current_min + arr[i], arr[i]); | ||
min_so_far = std::min(min_so_far, current_min); | ||
} | ||
|
||
if (min_so_far == sum) | ||
return max_so_far; | ||
|
||
// Return the maximum value | ||
return std::max(max_so_far, sum - min_so_far); | ||
} | ||
sumanbmondal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} // namespace dynamic_programming | ||
|
||
/** | ||
* @brief Self-test implementation | ||
* @returns void | ||
*/ | ||
static void test() { | ||
// Description of the test | ||
// Input: arr[] = {8, -8, 9, -9, 10, -11, 12} | ||
// Output: 22 | ||
// Explanation: Subarray 12, 8, -8, 9, -9, 10 gives the maximum sum, that is 22. | ||
|
||
int n = 7; // size of the array | ||
std::vector<int> arr = {8, -8, 9, -9, 10, -11, 12}; | ||
assert(dynamic_programming::maxCircularSum(arr) == 22); // this ensures that the algorithm works as expected | ||
|
||
arr = {8, -8, 10, -9, 10, -11, 12}; | ||
assert(dynamic_programming::maxCircularSum(arr) == 23); | ||
sumanbmondal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::cout << "All tests have successfully passed!\n"; | ||
} | ||
|
||
|
||
/** | ||
* @brief Main function | ||
* @param argc commandline argument count (ignored) | ||
* @param argv commandline array of arguments (ignored) | ||
* @returns 0 on exit | ||
*/ | ||
int main(int argc, char *argv[]) { | ||
sumanbmondal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test(); // run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.