Skip to content

Commit

Permalink
Added new ques in Summer Vacation DSA sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
KaranSiddhu committed Jun 6, 2022
1 parent 47c20c8 commit 7ed1680
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,45 @@ using namespace std;
#define ld long double
#define el "\n"

/*
1
6
-5 4 6 -3 4 -1
*/

//Using brute force approach ,Time Complexity: O(n^2)
/*ll maxSubarraySum(int arr[], int n){
ll maxSum = LONG_LONG_MIN;
for (int i = 0; i < n; i++){
ll tempSum = arr[i];
for (int j = i + 1; j < n; j++){
tempSum += arr[j];
if (tempSum > maxSum)
maxSum = tempSum;
}
}
return maxSum;
}*/

ll maxSubarraySum(int arr[], int n){
ll maxSum = LONG_LONG_MIN;
ll currSum = 0;

for (int i = 0; i < n; i++){
currSum += arr[i];

if(currSum > maxSum)
maxSum = currSum;

if(currSum < 0)
currSum = 0;
}

return maxSum;
}

int main(){
Expand All @@ -22,8 +59,8 @@ int main(){
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Solution - " << maxSubarraySum(arr, n);

cout << "Solution - " << maxSubarraySum(arr, n) << el;
}

return 0;
Expand Down

0 comments on commit 7ed1680

Please sign in to comment.