From c77c7f03b1e8253b3640589da4885779b606b2a4 Mon Sep 17 00:00:00 2001 From: Pooja Date: Tue, 8 Dec 2020 20:48:39 +0530 Subject: [PATCH] Create week1_pooja --- Week1/Max Subarray/week1_pooja | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Week1/Max Subarray/week1_pooja diff --git a/Week1/Max Subarray/week1_pooja b/Week1/Max Subarray/week1_pooja new file mode 100644 index 0000000..cf701c6 --- /dev/null +++ b/Week1/Max Subarray/week1_pooja @@ -0,0 +1,18 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + + int max_sum = nums[0]; + int max_sum_so_far = nums[0]; + + for(int i = 1; i < nums.size(); i++) + { + max_sum_so_far = max(max_sum_so_far + nums[i], nums[i]); + + if(max_sum_so_far > max_sum) + max_sum = max_sum_so_far; + + } + return max_sum; + } +};