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.
#include<stdio.h>// Function to calculate the total height of the stackinttotalHeight(int*stack,intn){inttotal=0;for(inti=0;i<n;i++){total+=stack[i];}returntotal;}// Function to find the maximum possible height of equal stacksintequalStacks(inth1[],intn1,inth2[],intn2,inth3[],intn3){// Calculate the total height of each stackinttotal1=totalHeight(h1,n1);inttotal2=totalHeight(h2,n2);inttotal3=totalHeight(h3,n3);inttop1=0,top2=0,top3=0;// Pointers to track the top of each stack// Continue until all the stacks are of equal heightwhile(!(total1==total2&&total2==total3)){// Find the highest stackintmaxTotal=total1>total2?total1:total2;maxTotal=maxTotal>total3?maxTotal:total3;// Reduce the highest stack by its top elementif(total1==maxTotal){total1-=h1[top1++];}elseif(total2==maxTotal){total2-=h2[top2++];}elseif(total3==maxTotal){total3-=h3[top3++];}}// Return the height of any of the equal stacksreturntotal1;}intmain(){// Read inputintn1,n2,n3;scanf("%d %d %d",&n1,&n2,&n3);inth1[n1],h2[n2],h3[n3];for(inti=0;i<n1;i++){scanf("%d",&h1[i]);}for(inti=0;i<n2;i++){scanf("%d",&h2[i]);}for(inti=0;i<n3;i++){scanf("%d",&h3[i]);}// Calculate and print the resultprintf("%d\n",equalStacks(h1,n1,h2,n2,h3,n3));return0;}
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 →