You are viewing a single comment's thread. Return to all comments →
Simple way
public static void main(String[] args) { Stack stack1 = new Stack(); Stack stack2 = new Stack(); Stack stack3 = new Stack(); /// stack1.push(3); stack1.push(2); stack1.push(1); stack1.push(1); stack1.push(1); // stack2.push(4); stack2.push(3); stack2.push(2); // stack3.push(1); stack3.push(1); stack3.push(4); stack3.push(1);
int sum1 = 0;int sum2 = 0;int sum3 = 0; for(int i=0; i< stack1.size(); i++) sum1 += stack1.get(i); for(int i=0; i< stack2.size(); i++) sum2 += stack2.get(i); for(int i=0; i< stack3.size(); i++) sum3 += stack3.get(i); int i = 0, j = 0, k = 0; while(!(sum1 == sum2 && sum2 == sum3)) { if(sum1 > sum2 && sum1 > sum3) { sum1 -= stack1.get(i); i++; }else if(sum2 > sum3 && sum2 > sum1){ sum2 -= stack2.get(j); j++; }else { sum3 -= stack3.get(k); k++; } } System.out.println(sum1); }
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 →
Simple way
public static void main(String[] args) { Stack stack1 = new Stack(); Stack stack2 = new Stack(); Stack stack3 = new Stack(); /// stack1.push(3); stack1.push(2); stack1.push(1); stack1.push(1); stack1.push(1); // stack2.push(4); stack2.push(3); stack2.push(2); // stack3.push(1); stack3.push(1); stack3.push(4); stack3.push(1);