Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions data_structures/arrays/Kadane's Algorithm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: "Kadane's Algorithm"
sidebar_label: "Introduction to Kadane's Algorithm"
description: "Kadane's Algorithm efficiently finds the maximum sum of a contiguous subarray. It is a fundamental algorithm for solving problems involving subarray sums."
tags: [basic-dsa, data-structures, Kadane's Algorithm]
---

### Definition:

Kadane’s Algorithm is used to find the **maximum sum of a contiguous subarray** within a given array. It efficiently handles both positive and negative numbers and works in **O(n)** time, making it a crucial tool for solving optimization problems related to arrays.

### Characteristics:

- **Linear Time Complexity**:

- The algorithm processes the array in a single pass, ensuring **O(n)** time complexity.

- **Handles Negative Numbers**:

- It can deal with arrays containing both positive and negative numbers, ensuring the correct maximum sum is found.

- **Space Complexity: O(1)**
- Only a few extra variables are used, making the space usage constant.

### Python Implementation:

```python
def max_subarray(nums):
"""
Find the maximum sum of a contiguous subarray using Kadane's Algorithm.
Args:
nums (list): List of integers.
Returns:
int: Maximum sum of any contiguous subarray.
"""
max_sum = curr_sum = nums[0]
for num in nums[1:]:
curr_sum = max(num, curr_sum + num)
max_sum = max(max_sum, curr_sum)
return max_sum

# Example usage
if __name__ == "__main__":
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
result = max_subarray(arr)
print("Maximum subarray sum:", result) # Output: 6
```
Loading