-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumSubarray.cpp
More file actions
31 lines (26 loc) · 813 Bytes
/
maximumSubarray.cpp
File metadata and controls
31 lines (26 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int main(){
vector<int> vec;
cout << "Enter number of elements to insert in the vector: ";
int n;
cin >> n;
cout << "Enter the elements of the vector: " << endl;
for(int i = 0; i < n; i++){
int element;
cin >> element;
vec.push_back(element); // Insert elements into the vector
}
int currSum = 0, maxSum = INT_MIN;
for(int val : vec){
currSum += val;
maxSum = max(maxSum, currSum);
if(currSum < 0){ //Kadane's Algorithm step to reset currSum if it goes negative because negative sum will not contribute to maximum sum subarray
currSum = 0;
}
}
cout << "The maximum subarray sum is: " << maxSum << endl;
return 0;
}