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

Efficient implementation for maximum contiguous subarray sum by Kadane's algorithm. More...

#include <array>
#include <cassert>
#include <climits>
#include <iostream>
Include dependency graph for kadanes3.cpp:

Functions

template<std::size_t SIZE>
int max_subarray_sum (std::array< int64_t, SIZE > arr, uint64_t length)
 for IO operations
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Efficient implementation for maximum contiguous subarray sum by Kadane's algorithm.

Our task is to take length of array and then the whole array as input from the user and then calculate the maximum contiguos subarray sum for the input array, using the kadane's algorithm.

There can be a case that all the elements in the input array are negative. In that case, the least value among all elements is the maximum sum with subarray length = 1.

Author
Abhijeet Tiwari

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
60 {
61 // Below is the code for accepting array from the user and then
62 // calling the function for the required output.
63 // It has been commented for now so that the test() function can run
64 // and the test cases can be verified.
65 // code for accepting array from user starts
66
67 // std::size_t n; // variable for length of input array
68 // std::cout << "Enter length of the array: ";
69 // std::cin >> n;
70 // std::array<int64_t, 100> arr = {0};
71 // // we need to give a constant in size. Hence we have allocated 100
72 // for now.
73 // for (int i = 0; i < n; i++)
74 // taking input of each element of the array
75 // {
76 // std::cin >> arr[i];
77 // }
78 // code for accepting array from user ends
79
80 // int max_sum = max_subarray_sum(arr, n);
81 // std::cout << "Maximum contiguous sum for this array is : " << max_sum
82 // << std::endl;
83
84 test(); // run self-test implementations
85 return 0;
86}
static void test()
Self-test implementations.
Definition kadanes3.cpp:48
Here is the call graph for this function:

◆ max_subarray_sum()

template<std::size_t SIZE>
int max_subarray_sum ( std::array< int64_t, SIZE > arr,
uint64_t length )

for IO operations

for std::array for assert for INT_MIN value

Utility function to check the current maximum number

Parameters
arrinput array
lengthlength of the input array
Returns
maximum contiguous subarray sum
29 {
30 int64_t current_max = INT_MIN, current_sum = 0;
31 for (int i = 0; i < length; i++) {
32 current_sum = current_sum + arr[i];
33 if (current_max < current_sum) {
34 current_max = current_sum;
35 }
36
37 if (current_sum < 0) {
38 current_sum = 0;
39 }
40 }
41 return current_max;
42}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
48 {
49 std::array<int64_t, 4> arr = {1, 2, 3, 4};
50 std::array<int64_t, 5> arr1 = {-1, -2, -4, -6, 7};
51 assert(max_subarray_sum(arr, 4) == 10);
52 assert(max_subarray_sum(arr1, 5) == 7);
53 std::cout << "All test cases have passed!\n";
54}
int max_subarray_sum(std::array< int64_t, SIZE > arr, uint64_t length)
for IO operations
Definition kadanes3.cpp:29
Here is the call graph for this function: