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.
There is a much simpler java solution without the need to have an additional set of stacks. We can simply mimic stack behavior using arrays. Here is the code to do that :
publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn1=in.nextInt(),i;intn2=in.nextInt();intn3=in.nextInt();//store heights of the stacks in these variablesinth1=0,h2=0,h3=0;//use these arrays to store the cylinder values //and mimic a stack DSint[]a1=newint[n1],a2=newint[n2],a3=newint[n3];for(i=0;i<n1;i++){intval=in.nextInt();a1[i]=val;h1+=val;}for(i=0;i<n2;i++){intval=in.nextInt();a2[i]=val;h2+=val;}for(i=0;i<n3;i++){intval=in.nextInt();a3[i]=val;h3+=val;}inttop1=0,top2=0,top3=0;intval;while(h1!=h2||h2!=h3){inth=Math.max(Math.max(h1,h2),h3);//popping the stack with the maximum total heightif(h==h1){val=a1[top1++];h1-=val;}elseif(h==h2){val=a2[top2++];h2-=val;}elseif(h==h3){val=a3[top3++];h3-=val;}}System.out.println(h1);}
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 →
There is a much simpler java solution without the need to have an additional set of stacks. We can simply mimic stack behavior using arrays. Here is the code to do that :