From d8df3b05290bcd8806db4d565abdf993cd4cad5c Mon Sep 17 00:00:00 2001 From: Ayushi C <67193440+AyushiChakrabarty@users.noreply.github.com> Date: Thu, 17 Sep 2020 23:17:40 +0530 Subject: [PATCH] Create Brute Force_Maximum Sum Subarray --- Brute Force_Maximum Sum Subarray | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Brute Force_Maximum Sum Subarray diff --git a/Brute Force_Maximum Sum Subarray b/Brute Force_Maximum Sum Subarray new file mode 100644 index 0000000..18cf192 --- /dev/null +++ b/Brute Force_Maximum Sum Subarray @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n^3) +{ + int ans = INT_MIN; // #include + 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<