Repository containing solution for #SdeSheetChallenge by striver
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.
for(i=0,n)
for (j=i,n)
sum=0
for(k=i,j)
sum+=arr[k]
check for max subarray sum
for(i=0,n)
sum=0;
for (j=i,n)
sum+= arr[i];
maxS=max(maxS,sum);
// codestudio
#include <bits/stdc++.h>
long long maxSubarraySum(int arr[], int n)
{
long long maxSum = 0;
long long currSum = 0;
for (int i = 0; i < n; i++) {
currSum += arr[i];
maxSum = max(maxSum, currSum); // end = i
currSum = max(0ll, currSum); // start = i+1
}
return maxSum;
}
start
pointer to i+1
.end
pointer to i
.