From 6201dd5e1479564028ab98e2c6481a308906b51c Mon Sep 17 00:00:00 2001 From: Nishant Agrawal Date: Sun, 31 Oct 2021 17:13:50 +0530 Subject: [PATCH 1/2] Added solution code for finding maximum sum of max sum subarray --- README.md | 10 ++++++++ .../kadanes-algorithm.cpp | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 find-sum-of-maximum-sum-subarray/kadanes-algorithm.cpp diff --git a/README.md b/README.md index c183e03..caebdfb 100644 --- a/README.md +++ b/README.md @@ -162,4 +162,14 @@ The majority element is the element that appears more than ⌊n / 2⌋ times. Yo Input :- [2,3,3,2,2] Output :- 2 +``` + +## [Find Majority Element](https://leetcode.com/problems/majority-element/) + +``` +Given an array of integers containing positive as well as negative integers. Find the sum of subarray which has maximum sum, in the given array. + +Input : 2,-3,4,5,-1 +Output : 9 (Subarray - [2,3]) + ``` \ No newline at end of file diff --git a/find-sum-of-maximum-sum-subarray/kadanes-algorithm.cpp b/find-sum-of-maximum-sum-subarray/kadanes-algorithm.cpp new file mode 100644 index 0000000..b810ac1 --- /dev/null +++ b/find-sum-of-maximum-sum-subarray/kadanes-algorithm.cpp @@ -0,0 +1,25 @@ +/* + +This is O(N) in time complexity. + +*/ + +#include +using namespace std; + +int maxSubArray(vector& arr) { + int curr_sum=0, max_sum=INT_MIN; + for(int i=0; i arr{2,-3,4,5,-1}; + int res = maxSubArray(arr); + cout<<"Sum of Maximum sum subarray is "< Date: Sun, 31 Oct 2021 17:28:41 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index caebdfb..11518b5 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ Output :- 2 ``` -## [Find Majority Element](https://leetcode.com/problems/majority-element/) +## [Find sum of Maximum sum subarray](https://leetcode.com/problems/maximum-subarray/) ``` Given an array of integers containing positive as well as negative integers. Find the sum of subarray which has maximum sum, in the given array. @@ -172,4 +172,4 @@ Given an array of integers containing positive as well as negative integers. Fin Input : 2,-3,4,5,-1 Output : 9 (Subarray - [2,3]) -``` \ No newline at end of file +```