forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadane.cpp
More file actions
32 lines (28 loc) · 743 Bytes
/
Kadane.cpp
File metadata and controls
32 lines (28 loc) · 743 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
32
//MAXIMUM SUM OF SUBARRAY(CONTIGUOUS SUBSEQUENCE) KADANE'S ALGO
#include <bits/stdc++.h>
using namespace std;
int kadmax(int arr[], int size)
{
int finalMax = INT_MIN, currentMax = 0; //INT_MIN = MOST NEGATIVE INTEGER
for(int i = 0; i < size; i++)
{
currentMax = currentMax + arr[i];
if(finalMax < currentMax)
finalMax = currentMax;
if(currentMax < 0)
currentMax = 0;
}
return finalMax;
}
int main()
{
int len, ans;
cout << "Enter length of Array : ";
cin >> len;
int ar[len];
cout<<"Enter elements of Array\n";
for(int i = 0; i < len; i++)
cin >> ar[i];
ans = kadmax(ar, len);
cout<<ans<<endl;
}