-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_maximum_sum_possible_equal_sum_of_three_stacks.cpp
61 lines (50 loc) · 1.45 KB
/
Find_maximum_sum_possible_equal_sum_of_three_stacks.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
#include <bits/stdc++.h>
using namespace std;
int maxSum(int s1[], int s2[], int s3[], int n1, int n2, int n3)
{
int sum1 = 0, sum2 = 0, sum3 = 0;
int top1 = 0, top2 = 0, top3 = 0;
int answer = 0;
for (int i = 0; i < n1; i++)
sum1 += s1[i];
for (int i = 0; i < n2; i++)
sum2 += s2[i];
for (int i = 0; i < n3; i++)
sum3 += s3[i];
while (true)
{
// The condition when we have not find any sum.
if (top1 == n1 || top2 == n2 || top3 == n3)
{
answer = 0;
break;
}
else if (sum1 == sum2 and sum2 == sum3)
{
answer = sum1;
break;
}
// The condition when sum1 is largest, then
// subtract its top from the sum and shift the top
else if (sum1 >= sum2 and sum1 >= sum3)
sum1 -= s1[top1++];
// The condition when sum2 is largest
else if (sum2 >= sum3 and sum2 >= sum1)
sum2 -= s2[top2++];
// The condition when sum3 is largest
else if (sum3 >= sum1 and sum3 >= sum2)
sum3 -= s3[top3++];
}
return answer;
}
int main()
{
int s1[] = {3, 2, 1, 1, 1};
int s2[] = {4, 3, 2};
int s3[] = {1, 1, 4, 1};
int n1 = sizeof(s1) / sizeof(s1[0]);
int n2 = sizeof(s2) / sizeof(s2[0]);
int n3 = sizeof(s3) / sizeof(s3[0]);
cout << "Maximum sum possible is : " << maxSum(s1, s2, s3, n1, n2, n3) << endl;
return 0;
}