Skip to content

Commit d0db094

Browse files
authored
Create Max Sum Subarray of Size K.py
1 parent f29c14d commit d0db094

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
3+
Max Sum Subarray of size K
4+
https://practice.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1
5+
6+
'''
7+
8+
class Solution:
9+
def maximumSumSubarray (self,K,Arr,N):
10+
# code here
11+
total = 0
12+
for i in range(K):
13+
total += Arr[i]
14+
ans = total
15+
i = K
16+
while i < len(Arr):
17+
total += Arr[i]
18+
total -= Arr[i-K]
19+
if total > ans:
20+
ans = total
21+
i += 1
22+
return ans
23+
24+
25+
if __name__ == '__main__':
26+
t = int (input ())
27+
for _ in range (t):
28+
29+
N,K = input().split()
30+
N = int(N)
31+
K = int(K)
32+
Arr = list( map(int,input().strip().split(" ")))
33+
34+
ob = Solution()
35+
print(ob.maximumSumSubarray(K,Arr,N))

0 commit comments

Comments
 (0)