diff --git a/algorithms/CPlusPlus/Arrays/Kadane's-Algorithm.cpp b/algorithms/CPlusPlus/Arrays/Kadane's-Algorithm.cpp new file mode 100644 index 00000000..ee05eb3d --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/Kadane's-Algorithm.cpp @@ -0,0 +1,82 @@ +/* Maximum contiguous subarray Sum of array brute to optimized all approaches */ + +#include +using namespace std; +//example int arr[3]={-1,3,-2}; +/*subarrays are + -1 + -1 3 + -1 3 -2 + 3 + 3 -2 + -2 + 3 + +ans=3 +*/ +int max_subarray_sum_method_1(int arr[],int n) +{ + int sum,maxi;//declaring variables + maxi=INT_MIN;//initializing maxi by minimum value; + for(int i=0;i>n; + int arr[n]; + for(int i=0;i>arr[i]; + } + int ans1=max_subarray_sum_method_1(arr,n);//this is brute force approach time complexity=O(n^3) || space complexity=O(1); + int ans2=max_subarray_sum_method_2(arr,n);//this is optimized brute force approach time complexity=O(n^2) || space complexity=O(1); + int ans3=max_subarray_sum_method_3(arr,n);//this is further optimized approach called "Kadane's Algorithm" time complexity=O(n)|| space complexity=O(1); + cout<<"max_subarray_sum:"<