-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarrays.cpp
147 lines (141 loc) · 3.79 KB
/
subarrays.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
using namespace std;
int max_subarray_sum(vector <int> arr){
int result = arr[0];
int local_result = arr[0];
for(int i = 1; i < arr.size(); i ++){
if(local_result > 0){
local_result += arr[i];
}
else{
local_result = arr[i];
}
if(local_result > result){
result = local_result;
}
}
return result;
}
int min_subarray_sum(vector <int> arr){
int result = arr[0];
int local_result = arr[0];
for(int i = 1; i < arr.size(); i ++){
if(local_result < 0){
local_result += arr[i];
}
else{
local_result = arr[i];
}
if(local_result < result){
result = local_result;
}
}
return result;
}
int max_subarray_prod(vector <int> arr){
int result = arr[0];
int local_result_pos = arr[0];
int local_result_neg = arr[0];
for(int i = 1; i < arr.size(); i ++){
int local_result_max, local_result_min;
if(local_result_pos * arr[i] > local_result_neg * arr[i]){
local_result_max = local_result_pos * arr[i];
local_result_min = local_result_neg * arr[i];
}
else{
local_result_max = local_result_neg * arr[i];
local_result_min = local_result_pos * arr[i];
}
if(arr[i] > local_result_max){
local_result_pos = arr[i];
}
else{
local_result_pos = local_result_max;
}
if(arr[i] < local_result_min){
local_result_neg = arr[i];
}
else{
local_result_neg = local_result_min;
}
if(local_result_pos > result){
result = local_result_pos;
}
}
return result;
}
int min_subarray_prod(vector <int> arr){
int result = arr[0];
int local_result_pos = arr[0];
int local_result_neg = arr[0];
for(int i = 1; i < arr.size(); i ++){
int local_result_max, local_result_min;
if(local_result_pos * arr[i] > local_result_neg * arr[i]){
local_result_max = local_result_pos * arr[i];
local_result_min = local_result_neg * arr[i];
}
else{
local_result_max = local_result_neg * arr[i];
local_result_min = local_result_pos * arr[i];
}
if(arr[i] > local_result_max){
local_result_pos = arr[i];
}
else{
local_result_pos = local_result_max;
}
if(arr[i] < local_result_min){
local_result_neg = arr[i];
}
else{
local_result_neg = local_result_min;
}
if(local_result_neg < result){
result = local_result_neg;
}
}
return result;
}
int max_circular_subarray_sum(vector <int> arr){
int max_elem = arr[0];
int arr_sum = 0;
for(int i = 0; i < arr.size(); i ++){
if(arr[i] > max_elem){
max_elem = arr[i];
}
arr_sum += arr[i];
}
if(max_elem < 0){
return max_elem;
}
int max_simple_sum = max_subarray_sum(arr);
int max_circle_sum = arr_sum - min_subarray_sum(arr);
if(max_simple_sum > max_circle_sum){
return max_simple_sum;
}
else{
return max_circle_sum;
}
}
int min_circular_subarray_sum(vector <int> arr){
int min_elem = arr[0];
int arr_sum = 0;
for(int i = 0; i < arr.size(); i ++){
if(arr[i] < min_elem){
min_elem = arr[i];
}
arr_sum += arr[i];
}
if(min_elem > 0){
return min_elem;
}
int min_simple_sum = min_subarray_sum(arr);
int min_circle_sum = arr_sum - max_subarray_sum(arr);
if(min_simple_sum < min_circle_sum){
return min_simple_sum;
}
else{
return min_circle_sum;
}
}