We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static int equalStacks(List h1, List h2, List h3) {
// Write your code here
Stack s1 = new Stack<>();
Stack s2 = new Stack<>();
Stack s3 = new Stack<>();
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
for (int i = h1.size()-1; i>=0;i--){
s1.push(h1.get(i));
sum1 += h1.get(i);
}
for (int i = h2.size()-1; i>=0;i--){
s2.push(h2.get(i));
sum2 += h2.get(i);
}
for (int i = h3.size()-1; i>=0;i--){
s3.push(h3.get(i));
sum3 += h3.get(i);
}
int min = Integer.MIN_VALUE;
while (sum1 != sum2 || sum2 != sum3){
min = Math.min(sum1,Math.min(sum2,sum3));
if (sum1>min ){
sum1 = sum1 - s1.pop();
}
if (sum2>min ){
sum2 = sum2 - s2.pop();
}
if (sum3>min ){
sum3 = sum3 - s3.pop();
}
}
return sum1;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
JAVA Solution
public static int equalStacks(List h1, List h2, List h3) { // Write your code here Stack s1 = new Stack<>(); Stack s2 = new Stack<>(); Stack s3 = new Stack<>(); int sum1 = 0; int sum2 = 0; int sum3 = 0;