-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathlargest_sum_subarray.cpp
More file actions
53 lines (50 loc) · 1.19 KB
/
Copy pathlargest_sum_subarray.cpp
File metadata and controls
53 lines (50 loc) · 1.19 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
@Author - Jatin Goel
@Institute - IIIT Allahabad
Hardwork definitely pays off.
There is no substitute of hardwork.
There is no shortcut to success.
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F first
#define S second
#define fast_io ios::sync_with_stdio(false);cin.tie(NULL)
int largest_sum_subarray(int a[],int n)
{
int max_so_far = 0;
int max_ending_here = 0;
for(int i=0;i<n;i++)
{
max_ending_here += a[i];
if(max_ending_here < 0)
max_ending_here = 0;
if(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
if(max_so_far == 0)
{
max_so_far = -INT_MAX;
for(int i=0;i<n;i++)
{
max_so_far = max(max_so_far,a[i]);
}
}
return max_so_far;
}
int main()
{
fast_io;
if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r"))
{
freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin);
freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout);
}
int n;
cin >> n;
int a[n+5];
for (int i=0;i<n;i++)
cin >> a[i];
cout << largest_sum_subarray(a,n) << endl;
}