8000 Added 1 DP and 1 Greedy algorithms code in cpp by Reetesh-Tomar · Pull Request #357 · AllAlgorithms/cpp · GitHub
[go: up one dir, main page]

Skip to content

Added 1 DP and 1 Greedy algorithms code in cpp #357

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Added Kadane algo code in cpp
  • Loading branch information
Reetesh-Tomar authored Oct 17, 2022
commit e615b8709dcd9db42a1a59e67668d3c6791c5c85
23 changes: 23 additions & 0 deletions algorithms/greedy/Kadane_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//Max subarray sum using Kadanes algorithm

#include <iostream>
using namespace std;

int main()
{
int arr_size; cin>>arr_size;
int arr[arr_size];
for(int i=0;i<arr_size;++i) cin>>arr[i];

int ma=0,ans=INT_MIN;
for(int i=0;i<arr_size;++i){
ma+=arr[i];
if(ma>ans){
ans=ma;
}
if(ma<0) ma=0;
}
cout<<ans<<endl;

return 0;
}
0