Skip to content

Commit

Permalink
Kadane's Alorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhavanak021 committed Oct 2, 2021
1 parent 7ebe1f6 commit c8ff43d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions DP/KadanesAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Kadane's Algorithm
Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one number)
which has the maximum sum and return its sum.
Example 1:
Input:
N = 5
Arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.
Example 2:
Input:
N = 4
Arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1
of element (-1)
*/
#include<iostream>
using namespace std;

int maxSum(int a[], int n){
int ma = INT_MIN;
int max_th = 0;
for(int i=0; i<n; i++){
max_th = max_th+ a[i];
if(max_th>ma)
ma = max_th;
if(max_th<0)
max_th = 0;
}
return ma;
}
int main(){
int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++)
cin>>a[i];
int sum= maxSum(a, n);
cout<<"Sum: "<<sum;
}

0 comments on commit c8ff43d

Please sign in to comment.