Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kadane's algorithm in short using dp #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions Algorithms/Dynamic Programming/Kadane's Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
#define int long long
#define INF 1000000000000000000
#define MOD 1000000009;
#define mid(l, u) ((l + u) / 2)
#define rchild(i) (i * 2 + 2)
#define lchild(i) (i * 2 + 1)
using namespace std;

signed main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int dp[n];
dp[0] = a[0];
int ans = a[0];
for (int i = 1; i < n; i++)
ans = max(ans, (dp[i] = a[i] + max(dp[i - 1], (int)0)));
cout << ans << endl;
}

/*
Sample Input:
8
-2 -3 4 -1 -2 1 5 -3
Sample Output:
7
*/