From cb3c86d72f52d3b767e1cd4f60a142e8b4f886bd Mon Sep 17 00:00:00 2001 From: "Abhay ;)" <145827052+Abhay182005dat@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:05:04 +0530 Subject: [PATCH 1/3] Create Kadanes.md --- docs/dsa/Kadanes.md | 100 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/dsa/Kadanes.md diff --git a/docs/dsa/Kadanes.md b/docs/dsa/Kadanes.md new file mode 100644 index 000000000..984592fc4 --- /dev/null +++ b/docs/dsa/Kadanes.md @@ -0,0 +1,100 @@ +--- +id: Algorithms +title: Kadanes Algorithm +sidebar_label: Algorithms +sidebar_position: 2 +description: "Kadane's Algorithm efficiently finds the maximum sum contiguous subarray in a one-dimensional array. It's a dynamic programming technique named after its creator, Jay Kadane." +--- +## Kadane's Algorithm: + +- **Purpose**: Finds the maximum sum of a contiguous subarray. +- **Time Complexity**: O(n) +- **Space Complexity**: O(1) +- **Use Case**: Optimal for solving maximum subarray sum problems in linear time. +### Simple Explanation +Kadane's Algorithm: + +1. **Initialize**: + - Set `maxSum` to a very small number (e.g., negative infinity). + - Set `currentSum` to 0. + +2. **Iterate Through the Array**: + - For each element in the array: + - Add the element to `currentSum`. + - Update `maxSum` if `currentSum` is greater than `maxSum`. + - If `currentSum` becomes negative, reset it to 0. + +3. **Result**: + - The value of `maxSum` at the end of the iteration is the maximum sum of any contiguous subarray. +### Code + +Input: arr = {-2,-3,4,-1,-2,1,5,-3} + +Output: 7 + +Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7. + +### Python + + def max_sub_array(nums): + max_sum = float('-inf') + current_sum = 0 + + for num in nums: + # Add the current number to the current_sum + current_sum += num + # Update max_sum if current_sum is greater + max_sum = max(max_sum, current_sum) + # Reset current_sum to 0 if it drops below 0 + if current_sum < 0: + current_sum = 0 + + return max_sum + +### C++ + // Kadane's Algorithm to find the maximum sum of a contiguous subarray + + #include + #include + #include + #include + + int maxSubArray(const std::vector& nums) { + // Initialize max_sum to the lowest possible value and current_sum to 0 + int max_sum = INT_MIN; + int current_sum = 0; + + for (int num : nums) { + // Add the current number to the current_sum + current_sum += num; + // Update max_sum if current_sum is greater + max_sum = std::max(max_sum, current_sum); + // Reset current_sum to 0 if it drops below 0 + if (current_sum < 0) { + current_sum = 0; + } + } + + return max_sum; + } + +### Explanation +Explanation: + +#### Initialization: + + -`max_sum` is initialized to `INT_MIN` to handle arrays with all negative numbers. + + -`current_sum` starts at `0`. + +#### Iteration: + + -Traverse through each number in the array: + + -Add the number to `current_sum`. + + -Update `max_sum` to be the maximum of `max_sum` and `current_sum`. + + -If `current_sum` becomes negative, reset it to `0` to start a new subarray. + + From 3cd77625efe914414f1783c76a497f95202531b1 Mon Sep 17 00:00:00 2001 From: "Abhay ;)" <145827052+Abhay182005dat@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:33:54 +0530 Subject: [PATCH 2/3] Update Kadanes.md --- docs/dsa/Kadanes.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/dsa/Kadanes.md b/docs/dsa/Kadanes.md index 984592fc4..50e913bc6 100644 --- a/docs/dsa/Kadanes.md +++ b/docs/dsa/Kadanes.md @@ -1,14 +1,14 @@ --- -id: Algorithms +id:Kadanes-Algorithm title: Kadanes Algorithm -sidebar_label: Algorithms +sidebar_label: KadanesAlgorithm sidebar_position: 2 description: "Kadane's Algorithm efficiently finds the maximum sum contiguous subarray in a one-dimensional array. It's a dynamic programming technique named after its creator, Jay Kadane." --- ## Kadane's Algorithm: - **Purpose**: Finds the maximum sum of a contiguous subarray. -- **Time Complexity**: O(n) +- **Time Complexity**: $O(n)$ - **Space Complexity**: O(1) - **Use Case**: Optimal for solving maximum subarray sum problems in linear time. ### Simple Explanation @@ -35,6 +35,7 @@ Output: 7 Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7. ### Python +```py def max_sub_array(nums): max_sum = float('-inf') @@ -50,8 +51,10 @@ Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7. current_sum = 0 return max_sum +``` ### C++ +```cpp // Kadane's Algorithm to find the maximum sum of a contiguous subarray #include @@ -77,6 +80,7 @@ Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7. return max_sum; } +``` ### Explanation Explanation: From cad4f7566aec3959d76b912ec84258313d13731a Mon Sep 17 00:00:00 2001 From: "Abhay ;)" <145827052+Abhay182005dat@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:37:05 +0530 Subject: [PATCH 3/3] Update Kadanes.md --- docs/dsa/Kadanes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dsa/Kadanes.md b/docs/dsa/Kadanes.md index 50e913bc6..e4eda5b2d 100644 --- a/docs/dsa/Kadanes.md +++ b/docs/dsa/Kadanes.md @@ -1,5 +1,5 @@ --- -id:Kadanes-Algorithm +id: Kadanes-Algorithm title: Kadanes Algorithm sidebar_label: KadanesAlgorithm sidebar_position: 2