Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Algorithms/MaxSubarraySum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

long long maxSubarraySum(int arr[], int n) {
long long maxi = LONG_MIN; // maximum sum
long long sum = 0;

for (int i = 0; i < n; i++) {

sum += arr[i];

if (sum > maxi) {
maxi = sum;
}

// If sum < 0: discard the sum calculated
if (sum < 0) {
sum = 0;
}
}

// To consider the sum of the empty subarray
// uncomment the following check:

//if (maxi < 0) maxi = 0;

return maxi;
}

int main()
{
int arr[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4};
int n = sizeof(arr) / sizeof(arr[0]);
long long maxSum = maxSubarraySum(arr, n);
cout << "The maximum subarray sum is: " << maxSum << endl;
return 0;
}
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ United Kingdom - https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/
|[Naveen Kumar .v](https://github.com/NK-dev-24) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I'm a Student. |
|[Chandan Arya](https://github.com/alpha2lucifer) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I am a passionate developer with a keen interest in blockchain technology and decentralized applications. My focus lies in creating efficient and secure solutions, and I enjoy exploring various programming languages and frameworks to build innovative applications.|
|[Tushar khatri](https://github.com/tusharkhatri434) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I'm a Frontend developer (ReactJs) |
|[Rudranil Ghosh](https://github.com/rudra1ghosh) | <img src="https://github.com/cu-sanjay/HacktoberFest2023/assets/96792511/5adfd776-ce7e-4467-92dd-727a12239cd1" alt="India" width="32" height="32"> | I'm a programmer, learning one step at a time. |