forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumSubArray.cpp
180 lines (176 loc) · 4.23 KB
/
MaximumSubArray.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <algorithm>
#include<iostream>
#include<iomanip>
#include<chrono>
#include<fstream>
#include<stdlib.h>
using namespace std;
using namespace std::chrono;
//structure to store the data of subarray
struct MaxSubArrayRecord
{
int start; //start index
int end; //end index
int sum; //sum of array
};
MaxSubArrayRecord Cross_Sum(int arr[], const int startIndex, const int midIndex, const int endIndex)
{
int leftSum = 0;
int rightSum = 0;
int currentSum = 0;
MaxSubArrayRecord result;
leftSum = arr[midIndex];
currentSum = arr[midIndex];
result.start = midIndex;
for (int i = midIndex - 1; i >= startIndex; i--)
{
currentSum = currentSum + arr[i];
if (currentSum > leftSum)
{
leftSum = currentSum;
result.start = i;
}
}
currentSum = 0;
rightSum = arr[midIndex + 1];
currentSum = arr[midIndex + 1];
result.end = midIndex + 1;
for (int i = midIndex + 2; i <= endIndex; i++)
{
currentSum = currentSum + arr[i];
if (currentSum > rightSum)
{
rightSum = currentSum;
result.end = i;
}
}
result.sum = leftSum + rightSum;
return result;
}
//recursive maxsubarray function to find the maximum subarray
MaxSubArrayRecord MaxSubArray(int arr[], const int startIndex, const int endIndex)
{
if (endIndex == startIndex) //if array consist of single element
{
MaxSubArrayRecord result; //object to store the result
result.start = startIndex;
result.end = endIndex;
result.sum = arr[startIndex];
return result;
}
else
{
int midIndex = (startIndex + endIndex) / 2;
MaxSubArrayRecord left;
MaxSubArrayRecord right;
MaxSubArrayRecord cross;
left = MaxSubArray(arr, startIndex, midIndex);
right = MaxSubArray(arr, midIndex + 1, endIndex);
cross = Cross_Sum(arr, startIndex, midIndex, endIndex);
if (left.sum >= right.sum && left.sum >= cross.sum)
{
return left;
}
else if (right.sum >= left.sum && right.sum >= cross.sum)
{
return right;
}
else
{
return cross;
}
}
}
//finding Maximum subArray using Brute Force algorithm
MaxSubArrayRecord MaxSubArrayBrute(const int arr[], const int size)
{
int l = 0;
int h = 0;
int m = arr[0];
int c;
MaxSubArrayRecord res;
for (int i = 0; i < size; i++)
{
c = 0;
for (int j = i; j < size; j++)
{
c = c + arr[j];
if (c > m)
{
m = c;
l = i;
h = j;
}
}
}
res.start = l;
res.end = h;
res.sum = m;
return res;
}
//depending on the size of the array mixed array function will call either brute force or recursive function
MaxSubArrayRecord MaxSubArrayMixed(int arr[], const int startIndex, const int endIndex)
{
int size = endIndex - startIndex + 1;
if (size < 30)
{
return MaxSubArrayBrute(arr, size);
}
else
{
int midIndex = (startIndex + endIndex) / 2;
MaxSubArrayRecord left;
MaxSubArrayRecord right;
MaxSubArrayRecord cross;
left = MaxSubArray(arr, startIndex, midIndex);
right = MaxSubArray(arr, midIndex + 1, endIndex);
cross = Cross_Sum(arr, startIndex, midIndex, endIndex);
if (left.sum >= right.sum && left.sum >= cross.sum)
{
return left;
}
else if (right.sum >= left.sum && right.sum >= cross.sum)
{
return right;
}
else
{
return cross;
}
}
}
//Function to generate random numbers
void RandomArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = (i * 45 + 79) % 101;
}
}
//main function
int main()
{
const int startIndex = 0, endIndex = 30000;
int size;
int arr[endIndex];
MaxSubArrayRecord result;
RandomArray(arr, endIndex);
cout << "****************************************\n";
// Get starting timepoint
auto start = high_resolution_clock::now();
result = MaxSubArrayMixed(arr, startIndex, endIndex);
// Get ending timepoint
auto stop = high_resolution_clock::now();
// use duration cast method
auto duration = duration_cast<seconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " seconds" << endl;
cout << "**************************************\n";
size = (result.end - result.start) + 1;
//printing the starting index and ending index and sum of maximunsubarray
cout << "starting index of maximum subarray is " << result.start << endl;
cout << "ending index of maximum subarray is " << result.end << endl;
cout << "The maximum sum is " << result.sum << endl;
cout << "The length of the array is " << size << endl;
return 0;
}