Skip to content

Instantly share code, notes, and snippets.

@SuvidhaMalaviya
Created April 23, 2022 15:04
Show Gist options
  • Save SuvidhaMalaviya/a2c254ded00edcdccf3d2aa96c1624a0 to your computer and use it in GitHub Desktop.
Save SuvidhaMalaviya/a2c254ded00edcdccf3d2aa96c1624a0 to your computer and use it in GitHub Desktop.
You have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height.
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
class EqualStackResult {
public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
int sum1 = 0, sum2 = 0, sum3 = 0;
for(int i=0;i< h1.size();i++){
sum1 +=h1.get(i);
}
for (int i=0;i<h2.size();i++){
sum2+=h2.get(i);
}
for(int i=0;i<h3.size();i++){
sum3+=h3.get(i);
}
while(!h1.isEmpty() && !h2.isEmpty() && !h3.isEmpty()){
int min = Math.min(Math.min(sum1,sum2),sum3);
while (sum1 > min){
sum1 -= h1.get(0);
h1.remove(0);
}
while (sum2 > min){
sum2 -= h2.get(0);
h2.remove(0);
}
while (sum3 > min){
sum3 -= h3.get(0);
h3.remove(0);
}
if(sum1 == sum2 && sum1 == sum3)
return sum1;
}
/*while(!(sum1 == sum2 && sum1 == sum3)) {
System.out.println(sum1+" "+sum2+" "+sum3);
int max = Integer.MIN_VALUE;
if (sum1 >= sum2 && sum1 >= sum3) {
max = 1;
} else if (sum2 >= sum1 && sum2 >= sum3) {
max = 2;
} else if (sum3 >= sum1 && sum3 >= sum2) {
max = 3;
}
//1755042 1754973 1755030
switch (max) {
case 1:
System.out.println("h1 "+h1.get(0));
sum1 -= h1.get(0);
h1.remove(0);
break;
case 2:
System.out.println("h2 "+h2.get(0));
sum2 -= h2.get(0);
h2.remove(0);
break;
case 3:
System.out.println("h3 "+h3.get(0));
sum3 -= h3.get(0);
h3.remove(0);
break;
}
}*/
return 0;
}
}
public class EqualStacks {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n1 = Integer.parseInt(firstMultipleInput[0]);
int n2 = Integer.parseInt(firstMultipleInput[1]);
int n3 = Integer.parseInt(firstMultipleInput[2]);
List<Integer> h1 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> h2 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> h3 = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = EqualStackResult.equalStacks(h1, h2, h3);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment