Given an array of integers Arr of size N and a number K. Return the maximum sum of a subarray of size K.
NOTE*: A subarray is a contiguous part of any given array.
Example 1:
Input:
N = 4, K = 2
Arr = [100, 200, 300, 400]
Output:
700
Explanation:
Arr_{3 } + Arr_{4} =700,
which is maximum.
 
Example 2:
Input:
N = 4, K = 4
Arr = [100, 200, 300, 400]
Output:
1000
Explanation:
Arr_{1} + Arr_{2} + Arr_{3 } 
+ Arr_{4} =1000,
which is maximum.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumSubarray() which takes the integer k, vector Arr with size N, containing the elements of the array and returns the maximum sum of a subarray of size K.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=10^{6}
1<=K<=N

Source: geeksforgeeks | Difficulty: EASY
Tags: Amortized analysis

Range queries

Starter code: #User function Template for python3

class Solution:

    def maximumSumSubarray (self,K,Arr,N):

        # code here 

Solutions:
class Solution:

	def maximumSumSubarray(self, k, a, n):
		i = j = 0
		mx = sums = 0
		while j < len(a):
			sums += a[j]
			if j - i + 1 < k:
				j += 1
			else:
				mx = max(mx, sums)
				sums = sums - a[i]
				i += 1
				j += 1
		return mx


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		winsum = sum(Arr[:K])
		maxsum = winsum
		for i in range(N - K):
			winsum = winsum - Arr[i] + Arr[i + K]
			maxsum = max(maxsum, winsum)
		return maxsum


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		start = 0
		end = 0
		sum1 = 0
		ans = 0
		while end < len(Arr):
			sum1 += Arr[end]
			if end - start + 1 == K:
				ans = max(sum1, ans)
				sum1 -= Arr[start]
				start += 1
			end += 1
		return ans


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		wstart = 0
		s = 0
		wend = 0
		ans = 0
		while wend < N:
			s += Arr[wend]
			if wend - wstart + 1 == K:
				ans = max(ans, s)
				s -= Arr[wstart]
				wstart += 1
			wend += 1
		return ans


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		ws = 0
		we = 0
		s = 0
		ans = 0
		while we < N:
			s += Arr[we]
			if we - ws + 1 == K:
				ans = max(ans, s)
				s -= Arr[ws]
				ws += 1
			we += 1
		return ans


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		i = 0
		j = 0
		sum = 0
		mx = -1000000
		while j < N:
			sum += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				mx = max(sum, mx)
				sum -= Arr[i]
				i += 1
				j += 1
		return mx


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		i = 0
		j = 0
		sums = 0
		maxi = -10 ** 7
		while j < N:
			sums += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				maxi = max(maxi, sums)
				sums -= Arr[i]
				i += 1
				j += 1
		return maxi


class Solution:

	def maximumSumSubarray(self, k, a, N):
		sum = 0
		for i in range(0, K):
			sum += a[i]
		max_sum = sum
		start = 0
		for i in range(k, N):
			sum = sum + a[i] - a[start]
			start = start + 1
			if sum > max_sum:
				max_sum = sum
		return max_sum


class Solution:

	def maximumSumSubarray(self, k, arr, n):
		winsum = sum(arr[:k])
		maxsum = winsum
		for i in range(n - k):
			winsum = winsum - arr[i] + arr[i + k]
			if winsum > maxsum:
				maxsum = winsum
		return maxsum


class Solution:

	def maximumSumSubarray(self, k, l, n):
		winsum = sum(l[0:k])
		maxsub = winsum
		for i in range(0, n - k):
			winsum = winsum - l[i] + l[i + k]
			maxsub = max(winsum, maxsub)
		return maxsub


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		ws = sum(Arr[:K])
		ms = ws
		for i in range(N - K):
			ws = ws - Arr[i] + Arr[K + i]
			ms = max(ms, ws)
		return ms


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		if N < K:
			print('Invalid')
			return -1
		res = 0
		for i in range(K):
			res += Arr[i]
		cursum = res
		for i in range(K, N):
			cursum += Arr[i] - Arr[i - K]
			res = max(res, cursum)
		return res


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		s = sum(Arr[:K])
		v = s
		for i in range(N - K):
			s = s - Arr[i] + Arr[i + K]
			v = max(s, v)
		return v


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		l = sum(Arr[:K])
		k = l
		for i in range(N - K):
			l = l - Arr[i] + Arr[i + K]
			k = max(k, l)
		return k


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		max_sum = sum(Arr[:K])
		cur_sum = max_sum
		for i in range(0, N - K):
			cur_sum += Arr[K + i] - Arr[i]
			max_sum = max(max_sum, cur_sum)
		return max_sum


from typing import List

class Solution:

	def maximumSumSubarray(self, K: int, Arr: List[int], N: int) -> int:
		winsum = sum(Arr[:K])
		maxsum = winsum
		for i in range(N - K):
			winsum = winsum - Arr[i] + Arr[i + K]
			maxsum = max(maxsum, winsum)
		return maxsum


class Solution:

	def maximumSumSubarray(self, K, arr, N):
		win_sum = sum(arr[:K])
		maxsum = win_sum
		for i in range(0, N - K):
			win_sum = win_sum - arr[i] + arr[i + K]
			maxsum = max(win_sum, maxsum)
		return maxsum


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		(i, j, total, maxsum) = (0, 0, 0, 0)
		while j < len(Arr):
			total += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				maxsum = max(maxsum, total)
				total = total - Arr[i]
				i += 1
				j += 1
		return maxsum


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		win = sum(Arr[:K])
		m = win
		for i in range(N - K):
			win = win - Arr[i] + Arr[i + K]
			m = max(win, m)
		return m


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		res = sum(Arr[:K])
		cs = res
		for i in range(K, N):
			cs += Arr[i] - Arr[i - K]
			res = max(res, cs)
		return res


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		cur_sum = 0
		ans = 0
		cur_sum = sum(Arr[0:K])
		ans = cur_sum
		i = 0
		j = K
		while j < N:
			cur_sum = cur_sum + Arr[j] - Arr[i]
			ans = max(ans, cur_sum)
			i += 1
			j += 1
		return ans


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		if N < K:
			return -1
		sum = 0
		for i in range(K):
			sum = sum + Arr[i]
		csum = sum
		for i in range(K, N):
			csum = csum + Arr[i] - Arr[i - K]
			sum = max(sum, csum)
		return sum


class Solution:

	def maximumSumSubarray(self, K, arr, N):
		winsum = sum(arr[:K])
		ms = winsum
		for i in range(N - K):
			winsum = winsum - arr[i] + arr[i + K]
			ms = max(ms, winsum)
		return ms


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		mx = 0
		sumK = 0
		i = 0
		j = 0
		while j < len(Arr):
			sumK += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				mx = max(mx, sumK)
				sumK -= Arr[i]
				i += 1
				j += 1
		return mx


class Solution:

	def maximumSumSubarray(self, k, arr, n):
		wind = sum(arr[:k])
		maxsum = wind
		for i in range(n - k):
			wind = wind - arr[i] + arr[i + k]
			maxsum = max(maxsum, wind)
		return maxsum


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		res = 0
		curr = 0
		for i in range(K):
			curr = curr + Arr[i]
		if curr > res:
			res = curr
		for i in range(K, N):
			curr = curr + Arr[i] - Arr[i - K]
			if curr > res:
				res = curr
		return res


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		w = sum(Arr[:K])
		b = w
		for i in range(N - K):
			w = w - Arr[i] + Arr[i + K]
			b = max(w, b)
		return b


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		(l, r) = (0, 0)
		max_ = 0
		sum_ = 0
		while r < N:
			sum_ += Arr[r]
			if r >= K - 1:
				max_ = max(max_, sum_)
				sum_ -= Arr[l]
				l += 1
			r += 1
		return max_


class Solution:

	def maximumSumSubarray(self, k, nums, n):
		window = Arr[0:K]
		windosum = sum(window)
		maxi = windosum
		for i in range(0, N - K):
			windosum = windosum - Arr[i] + Arr[i + K]
			maxi = max(windosum, maxi)
		return maxi


from typing import List

class Solution:

	def maximumSumSubarray(self, K: int, Arr: List[int], N: int) -> int:
		window_sum = sum(Arr[:K])
		max_sum = window_sum
		for i in range(K, N):
			window_sum = window_sum + Arr[i] - Arr[i - K]
			max_sum = max(max_sum, window_sum)
		return max_sum


class Solution:

	def maximumSumSubarray(self, k, arr, n):
		i = j = ms = curr_sum = 0
		while j < n:
			curr_sum += Arr[j]
			if j - i + 1 == k:
				ms = max(ms, curr_sum)
				curr_sum -= arr[i]
				i += 1
			j += 1
		return ms


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		cursum = 0
		res = 0
		for (i, x) in enumerate(Arr):
			if i >= K:
				cursum = cursum - Arr[i - K]
			cursum = cursum + x
			res = max(res, cursum)
		return res


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		i = 0
		j = 0
		csum = 0
		maxi = 0
		while j < N:
			csum += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				maxi = max(maxi, csum)
				csum -= Arr[i]
				i += 1
				j += 1
		return maxi


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		ws = 0
		ms = 0
		s = 0
		for i in range(N):
			ws += Arr[i]
			if i - s + 1 == K:
				ms = max(ms, ws)
				ws -= Arr[s]
				s += 1
		return ms


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		if K < 0 or K > len(Arr):
			return -1
		else:
			sum = 0
			for i in range(0, K):
				sum += Arr[i]
			max_sum = sum
			start = 0
			for j in range(K, len(Arr)):
				sum = sum + Arr[j] - Arr[start]
				start += 1
				if sum > max_sum:
					max_sum = sum
			return max_sum


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		maxi = float('-inf')
		curr_sum = 0
		j = 0
		for i in range(K):
			curr_sum += Arr[i]
		maxi = curr_sum
		for i in range(K, len(Arr)):
			curr_sum = curr_sum - Arr[j] + Arr[i]
			maxi = max(curr_sum, maxi)
			j += 1
		return maxi


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		if N < K:
			return -1
		temp = 0
		for i in range(K):
			temp += Arr[i]
		maxsum = temp
		for i in range(K, N):
			maxsum += Arr[i] - Arr[i - K]
			temp = max(temp, maxsum)
		return temp


class Solution:

	def maximumSumSubarray(self, k, arr, n):
		res = 0
		curr = 0
		for i in range(k):
			curr += arr[i]
		if curr > res:
			res = curr
		for i in range(k, n):
			curr = curr + arr[i] - arr[i - k]
			if curr > res:
				res = curr
		return res


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		(i, j) = (0, 0)
		s = 0
		mx = float('-inf')
		while j < len(Arr):
			s += Arr[j]
			if j - i + 1 < K:
				j += 1
			elif j - i + 1 == K:
				mx = max(mx, s)
				s -= Arr[i]
				i += 1
				j += 1
		return mx


class Solution:

	def maximumSumSubarray(self, K, Arr, N):
		if K > N:
			return 0
		if K == N:
			return sum(Arr)
		i = j = 0
		res = 0
		for i in range(K):
			res += Arr[i]
		curr_sum = res
		for i in range(K, N):
			curr_sum += Arr[i] - Arr[i - K]
			res = max(res, curr_sum)
		return res


class Solution:

	def maximumSumSubarray(self, k, Arr, N):
		s = sum(Arr[:k])
		ans = s
		for i in range(k, N):
			s += Arr[i]
			s -= Arr[i - k]
			ans = max(s, ans)
		return ans