TheAlgorithms/C++ 1.0.0
All the 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:

Go to the source code of this file.

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.

Definition in file maximum_circular_subarray.cpp.

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

Definition at line 87 of file maximum_circular_subarray.cpp.

87 {
88 test(); // run self-test implementations
89 return 0;
90}
static void test()
Self-test implementation.

◆ test()

static void test ( )
static

Self-test implementation.

Returns
void

Definition at line 64 of file maximum_circular_subarray.cpp.

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