-
Notifications
You must be signed in to change notification settings - Fork 64
/
Kadane's_algorithm.cpp
48 lines (39 loc) · 993 Bytes
/
Kadane's_algorithm.cpp
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
//
// main.cpp
//
// Created by Prince Kumar on 21/04/20.
// Copyright © 2020 Prince Kumar. All rights reserved.
//
// ---** MAX SUM CONTIGUOUS SUBARRAY **---
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
cout<<"Enter the size of Array : "<<endl;
int n; cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int max_so_far = a[0], max_ending_here =0; // It store sum
int start = 0 , end=0 , s=0; // it store index
for(int i=0;i<n;i++){
max_ending_here+=a[i];
if(max_so_far < max_ending_here){
max_so_far=max_ending_here;
start = s; end=i;
}
if(max_ending_here<0){
max_ending_here = 0;
s = i+1;
}
}
int sum=0;
for(int i=start ;i<=end ; i++){
sum+=a[i];
cout<<a[i]<<" ";
}
cout<<endl;
cout<<sum<<endl;
}