forked from Geetika-2001/CodingSimplified
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadane_Algorithm_CPP.cpp
More file actions
68 lines (46 loc) · 1.9 KB
/
Kadane_Algorithm_CPP.cpp
File metadata and controls
68 lines (46 loc) · 1.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Author : shreyas
// McMurdo Station, Antartica
// Pengiun Intelligence Agency
// Date : 07-03-2981
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define time cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n" ;
#define F first
#define S second
#define pb push_back
typedef long long int ll ;
/***********************KADANE'S ALGORITHM******************************/
/*
This is algorithm is usually used to find maximum sum subarray in O(N) time complexity
Brute Force Approach :
Consider each element as a starting of new Subarray and calculate max sum for each element.
Time complexity : O (N^2)
*/
void solve() {
ll arr[]={4,3,-2,6,7,-10,-10,4,5,9,-3,4,7,-28,2,9,3,2,11} ;
ll n = sizeof(arr)/sizeof(arr[0]) ;
ll currentSum = arr[0] ; // currentSum calculates the current Sum in array
ll totalSum = arr[0] ; // totalSum calculates the max sum of a subarray
for(ll i = 1; i < n ; i++){
if(currentSum >= 0){
currentSum += arr[i] ; // if elements till index i are positive we will consider it as currentSum and add in it.
}
else{
currentSum = arr[i] ; // if currentSum till i-1 is negative , we will initialize currentSum with new element and start a new subarray
}
if(currentSum > totalSum){
totalSum = currentSum ; // at each index i we will challenge the totalSum , if currentSum is greater than totalSum . Then totalSum = currentSum
}
}
cout << totalSum <<"\n" ;
}
int32_t main() {
fast ; time;
int t = 1;
//cin >> t;
while (t--) {
solve() ;
}
return 0 ;
}