Skip to content

Commit 5b70301

Browse files
authored
Add files via upload
1 parent a823ed7 commit 5b70301

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Stack/EqualStacks.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Equal Stacks
3+
4+
You have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times.
5+
6+
Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of the three stacks until they're all the same height, then print the height. The removals must be performed in such a way as to maximize the height.
7+
8+
Note: An empty stack is still a stack.
9+
*/
10+
11+
12+
13+
import java.io.*;
14+
import java.util.*;
15+
import java.text.*;
16+
import java.math.*;
17+
import java.util.regex.*;
18+
19+
public class equalStacks {
20+
21+
public static void main(String[] args) {
22+
Scanner in = new Scanner(System.in);
23+
int n1 = in.nextInt();
24+
int n2 = in.nextInt();
25+
int n3 = in.nextInt();
26+
27+
int i=1;
28+
long sum1=0,sum2=0,sum3=0,max=0;
29+
30+
int [] a = new int [n1];
31+
int [] b = new int [n2];
32+
int [] c = new int [n3];
33+
34+
35+
for(i=0;i<n1;i++)
36+
{
37+
a[i]=in.nextInt();
38+
sum1=sum1+a[i];
39+
}
40+
41+
42+
43+
for(i=0;i<n2;i++)
44+
{
45+
b[i]=in.nextInt();
46+
sum2=sum2+b[i];
47+
}
48+
49+
for(i=0;i<n3;i++)
50+
{
51+
c[i]=in.nextInt();
52+
sum3=sum3+c[i];
53+
}
54+
55+
int x=0,y=0,z=0;
56+
57+
if(sum1==sum2 && sum2==sum3)
58+
max=sum1;
59+
60+
while(sum1!=sum2 || sum2!=sum3)
61+
{
62+
if((sum1>sum2 && sum1>sum3) || (sum1==sum2 && sum1>sum3) || (sum1==sum3 && sum1>sum2))
63+
{
64+
if(sum1==0)
65+
break;
66+
sum1 = sum1 - a[x++];
67+
}
68+
69+
else if((sum2>sum1 && sum2>sum3) || (sum2==sum1 && sum2>sum3) || (sum2==sum3 && sum2>sum1))
70+
{
71+
if(sum2==0)
72+
break;
73+
sum2 = sum2 - b[y++];
74+
}
75+
else if((sum3>sum1 && sum3>sum2) || (sum3==sum1 && sum3>sum2) || (sum3==sum2 && sum3>sum1))
76+
{
77+
if(sum3==0)
78+
break;
79+
sum3 = sum3 - c[z++];
80+
}
81+
else
82+
break;
83+
84+
if(sum1==sum2 && sum2==sum3)
85+
max=sum1;
86+
}
87+
88+
System.out.println(max);
89+
90+
}
91+
}

0 commit comments

Comments
 (0)