added max subarray sum (#131)

pull/134/head
Ritish Sehgal 2021-04-03 21:46:29 +05:30 committed by GitHub
parent 6cd32d216b
commit 84053d436f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -6,6 +6,7 @@
2. [Dutch Flag Algo](c-or-cpp/dutch-flag-algo.cpp)
3. [Left Rotation of Array](c-or-cpp/left-rotation.cpp)
4. [Shift Negatives in Array](c-or-cpp/shift-negatives.cpp)
5. [Maximum Subarray Sum](c-or-cpp/max-subarray-sum.cpp)
### Python
@ -18,4 +19,3 @@
### Java
1. [Counting Inversions](java/count-inversions.java)

View File

@ -0,0 +1,69 @@
#include<iostream>
#include<climits>
#define endl "\n"
using namespace std;
// Maximum Subarray Sum
// Approach A - Brute Force O(n^3)
int maxSubArrSum_A(int a[],int n){
int maxSum = INT_MIN;
for(int i=0;i<n;++i){
for(int j=i;j<n;++j){
int sum = 0;
for(int k=i;k<=j;++k){
sum = sum + a[k];
}
maxSum = max(maxSum,sum);
}
}
return maxSum;
}
// Appraoch B - Cumulative Sum Approach O(n^2)
int maxSubArrSum_B(int a[],int n){
int currSum[n+1]; currSum[0] = 0;
for(int i=1;i<=n;++i){
currSum[i] = currSum[i-1] + a[i-1];
}
int maxSum = INT_MIN;
for(int i=1;i<=n;++i){
for(int j=0;j<i;++j){
int sum = 0;
sum = currSum[i] - currSum[j];
maxSum = max(maxSum,sum);
}
}
return maxSum;
}
// Approach C - Kadane's Algo O(n)
int maxSubArrSum_C(int a[],int n){
int currSum=0;
int maxSum = INT_MIN;
for(int i=0;i<n;++i){
currSum += a[i];
if(currSum < 0) currSum = 0;
maxSum = max(maxSum,currSum);
}
return maxSum;
}
//Utility function to print array
void printArr(int a[],int n){
for(int i=0;i<n;++i) cout<<a[i]<<" ";
cout<<endl;
}
int main(){
//Some sample test cases
int a[] = {5,6,0,4,-1,4,7,2}; // n=8
int b[] = {4,-4,6,-6,10,-11,12}; // n=7
int c[] = {3,4,1,0,-2,-6,8,3}; // n=8
cout<<maxSubArrSum_A(b,7)<<endl;
cout<<maxSubArrSum_B(b,7)<<endl;
cout<<maxSubArrSum_C(b,7)<<endl;
return 0;
}