-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Brute Force_Maximum Sum Subarray
- Loading branch information
1 parent
74f9690
commit d8df3b0
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include<cmath> | ||
#include<iostream> | ||
#include<climits> | ||
using namespace std; | ||
|
||
int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n^3) | ||
{ | ||
int ans = INT_MIN; // #include<climits> | ||
for(int sub_array_size = 1;sub_array_size <= n; ++sub_array_size) //O(n) | ||
{ | ||
for(int start_index = 0;start_index < n; ++start_index) //O(n) | ||
{ | ||
if(start_index+sub_array_size > n) //Subarray exceeds array bounds | ||
break; | ||
int sum = 0; | ||
for(int i = start_index; i < (start_index+sub_array_size); i++) //O(n) | ||
sum+= arr[i]; | ||
ans = max(ans,sum); | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
int arr[] = {3,-2,5,-1}; | ||
cout<<MSS(arr,4)<<"\n"; | ||
return 0; | ||
} |