forked from kishanBhandary/Projects-and-Interview-Question
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkadanes_algorithm.py
More file actions
49 lines (36 loc) · 1.73 KB
/
kadanes_algorithm.py
File metadata and controls
49 lines (36 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Kadane's Algorithm Implementation in Python
def kadanes_algorithm(arr):
"""
Finds the maximum sum of a contiguous subarray in a given array of numbers.
The algorithm iterates through the array, keeping track of the maximum sum
ending at the current position and the overall maximum sum found so far.
Args:
arr: A list of integers (can contain positive and negative numbers).
Returns:
The integer value of the maximum subarray sum. Returns 0 if the array is empty.
"""
if not arr:
return 0
# Initialize variables
# max_so_far will store the maximum sum found anywhere in the array
# current_max will store the maximum sum of the subarray ending at the current position
max_so_far = arr[0]
current_max = arr[0]
# Iterate through the array starting from the second element
for i in range(1, len(arr)):
num = arr[i]
# Decide whether to extend the existing subarray or start a new one
# by comparing the current number with the sum of the current number and the previous max subarray
current_max = max(num, current_max + num)
# Update the overall maximum sum if the current subarray's sum is greater
max_so_far = max(max_so_far, current_max)
return max_so_far
# This block will be executed when the script is run directly
if __name__ == "__main__":
# Dummy data to test the algorithm
dummy_data = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(f"Original Array: {dummy_data}")
# Execute the algorithm with the dummy data
max_sum = kadanes_algorithm(dummy_data)
print(f"The maximum subarray sum is: {max_sum}")
# The subarray with the largest sum is [4, -1, 2, 1], which sums to 6.