def approach3(nums):
ans = nums[0]
subarr_sum = nums[0]
for i in range(1, len(nums)):
subarr_sum = max(nums[i], nums[i] + subarr_sum)
ans = max(ans, subarr_sum)
return ans
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_sum = INT_MIN;
int sum = 0;
for(int i = 0; i < nums.size(); i++){
sum += nums[i];
if(max_sum < sum){
max_sum = sum;
}
if(sum < 0){
sum = 0;
}
}
return max_sum;
}
};
public int maxSubArray(int[] nums) {
int result = nums[0];
int sum = nums[0];
for(int i=1; i<nums.length; i++){
sum = Math.max(nums[i], sum + nums[i]);
result = Math.max(result, sum);
}
return result;
}
arr = [6,7,7,41,5,3,1,3]
# arr = [1,2,3,4,5]
n = 3
def MaxSubarraySum(arr, n) -> int:
tempSum, maxSum = 0,0
length = len(arr)
for i in range(length):
tempSum += arr[i]
if i >= n-1:
maxSum = max(tempSum, maxSum)
tempSum -= arr[i - (n-1)]
print(maxSum)
MaxSubarraySum(arr,n)