- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaxSubArray.py
More file actions
Latest commit
32 lines (29 loc) · 1.03 KB
/
Copy pathmaxSubArray.py
File metadata and controls
32 lines (29 loc) · 1.03 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
#Kadane's Algorithm
#Program to find maximum sub array in an array
classmaxSubArray:
def__init__(self, inpArr):
self.inpArr=inpArr
deffindMaxSubArray(self):
maxSum=0
currentSum=0
currentEnd=0
currentStart=0
maxEndIndex=0
maxStartIndex=0
while (currentEnd<len(self.inpArr)):
currentSum+=self.inpArr[currentEnd]
if (currentSum>maxSum):
maxSum=currentSum
maxStartIndex=currentStart
maxEndIndex=currentEnd
if (currentSum<0):
currentSum=0
currentStart=currentEnd+1
currentEnd+=1
print ("maxSum: {0} \nmaxStartIndex: {1} \nmaxEndIndex: {2}".format(maxSum, maxStartIndex, maxEndIndex))
printself.inpArr[maxStartIndex:maxEndIndex+1]
if__name__:
inpArr= [1, 2, 3, -90, 6, 7, -2, 10,-123, 12]
#inpArr = [-2, 4, -2, 7, 4, -8, -6, 2, 9, 1, 0, -4]
obj=maxSubArray(inpArr)
obj.findMaxSubArray()