Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
maximum_circular_subarray.cpp File Reference

C++ program for maximum contiguous circular sum problem using Kadane's Algorithm More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for maximum_circular_subarray.cpp:

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 

Functions

int dynamic_programming::maxCircularSum (std::vector< int > &arr)
 returns the maximum contiguous circular sum of an array
 
static void test ()
 Self-test implementation.
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

C++ program for maximum contiguous circular sum problem using Kadane's Algorithm

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 explanation page.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit
87 {
88 test(); // run self-test implementations
89 return 0;
90}
static void test()
Self-test implementation.
Definition maximum_circular_subarray.cpp:64
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementation.

Returns
void
64 {
65 // Description of the test
66 // Input: arr[] = {8, -8, 9, -9, 10, -11, 12}
67 // Output: 22
68 // Explanation: Subarray 12, 8, -8, 9, -9, 10 gives the maximum sum, that is 22.
69
70 int n = 7; // size of the array
71 std::vector<int> arr = {8, -8, 9, -9, 10, -11, 12};
72 assert(dynamic_programming::maxCircularSum(arr) == 22); // this ensures that the algorithm works as expected
73
74 arr = {8, -8, 10, -9, 10, -11, 12};
75 assert(dynamic_programming::maxCircularSum(arr) == 23);
76
77 std::cout << "All tests have successfully passed!\n";
78}
int maxCircularSum(std::vector< int > &arr)
returns the maximum contiguous circular sum of an array
Definition maximum_circular_subarray.cpp:26
Here is the call graph for this function: