From f60402f101b6af77db05c22a4a2bb8106e7a50fe Mon Sep 17 00:00:00 2001 From: Yashgabani845 Date: Sun, 2 Jun 2024 17:50:35 +0530 Subject: [PATCH 1/2] kadane's algorithm solution added --- .../gfg-solutions/0003-kadane's-algorithm.md | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md diff --git a/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md new file mode 100644 index 000000000..c27a3ed9f --- /dev/null +++ b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md @@ -0,0 +1,270 @@ +--- +id: kadane's-algorithm +title: Kadane's Alogrithm (Geeks for Geeks) +sidebar_label: 0003 - Kadane's Algorithm +tags: + - intermediate + - Array + - Dynamic Programming + - Data Structure + - Algorithms + +description: "This is a solution to the Kadane's Algorithm problem on Geeks for Geeks." +--- + +This tutorial contains a complete walk-through of the Kadane's Algorithm problem from the Geeks for Geeks website. It features the implementation of the solution code in three programming languages: Python ,C++ ,java. + +## Problem Description + +Given an array Arr[] of N integers, find the contiguous sub-array (containing at least one number) which has the maximum sum and return its sum. + +## Examples + +**Example 1:** + +``` +Input: +N = 5 +Arr[] = {1,2,3,-2,5} +Output: +9 +Explanation: +Max subarray sum is 9 +of elements (1, 2, 3, -2, 5) which +is a contiguous subarray. +``` + +**Example 2:** + +``` +Input: +N = 4 +Arr[] = {-1,-2,-3,-4} +Output: +-1 +Explanation: +Max subarray sum is -1 +of element (-1) +``` + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function `maxSubarraySum()` which takes Arr[] and N as input parameters and returns the sum of subarray with maximum sum. + +Expected Time Complexity: $O(N)$ +Expected Auxiliary Space: $O(1)$ + +## Constraints + +1. $(1 \leq N \leq 10^6)$ +2. $(-10^7 \leq A[i] \leq 10^7)$ + +## Solution Approach + +### Brute Force Approach + +#### Intuition: +Try all possible subarrays by using nested loops and pick the subarray which has the maximum sum. + +#### Implementation: +1. Keep an answer variable to store the maximum subarray sum. +2. Run a loop(i). +3. Run a nested loop from i till the end of the array. +4. Generate all subarrays starting from the ith index and compare its sum with the answer and update the answer with the maximum one. +5. Return the answer. + +#### Code (C++): + +```cpp +class Solution{ + public: + // arr: input array + // n: size of array + //Function to find the sum of contiguous subarray with maximum sum. + long long maxSubarraySum(int arr[], int n){ + + // Your code here + long long ans=arr[0]; + for(int i=0;i Date: Sun, 2 Jun 2024 17:52:50 +0530 Subject: [PATCH 2/2] complexity style added --- dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md index c27a3ed9f..12ee2ecc8 100644 --- a/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md +++ b/dsa-solutions/gfg-solutions/0003-kadane's-algorithm.md @@ -250,7 +250,7 @@ class Solution: ``` #### Complexity: -- Time Complexity: $(O(N))$, where O(N)$ is the size of the array as we are looping once over the whole array. +- Time Complexity: $(O(N))$, where $O(N)$ is the size of the array as we are looping once over the whole array. - Space Complexity: $(O(1))$, Here we are not using extra space. ## Conclusion