-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC31ProblemSubArrayWhichHasMaximumSum.cpp
More file actions
68 lines (57 loc) · 1.44 KB
/
C31ProblemSubArrayWhichHasMaximumSum.cpp
File metadata and controls
68 lines (57 loc) · 1.44 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
68
#include<bits/stdc++.h>
using namespace std;
int32_t main(){
// Print SubArray Which Has Maximum Sum
int n;
cin>>n;
int arr[n];
for(int i = 0; i<n; i++){
cin>>arr[i];
}
// total subarray in size of N array id : N * (N + 1)/2.
int t = (n *(n + 1))/2;
// cout<<t;
int start[t] , end[t] , sum[t]; // 3arrays
int sum_ele = 0;
int start_ele = 0;
int end_ele = 0;
// Max variable staore maximum sum value
int max_value = INT_MIN;
int index = -1;
while(end_ele < n){
index += 1;
for(int i=start_ele; i<=end_ele; i++){
sum_ele += arr[i];
}
// Save in to an Array
start[index] = start_ele;
end[index] = end_ele;
sum[index] = sum_ele;
// Assign max value
max_value = max(max_value , sum_ele);
end_ele += 1;
// cout<<sum_ele<<endl; // sum
if(end_ele >= n){
start_ele += 1;
end_ele = start_ele;
}
sum_ele = 0;
}
/*
for(int i = 0; i<t; i++){
cout<<start[i]<<" ";
cout<<end[i]<<" ";
cout<<sum[i]<<endl;
}
cout<<max_value;
*/
for(int i=0; i<t; i++){
if(max_value == sum[i]){
for(int j=start[i]; j<=end[i]; j++){
cout<<arr[j]<<" ";
}
}
cout<<endl;
}
return 0;
}