HackerRank Equal Stacks problem solution YASH PAL, 31 July 2024 In this HackerRank Equal Stacks problem, we have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. you can change the height of a stack by removing and discarding its topmost cylinder any number of times. we need to find the maximum possible height of the stacks such that all of the stacks are exactly the same height. Problem solution in Python programming. def check_equal_heights(heights): h_0 = heights[0] for h_i in heights[1:]: if h_i != h_0: return False return True num_blocks = [int(num) for num in input().strip().split()] num_stacks = len(num_blocks) stacks = [] for __ in range(num_stacks): stack = [int(h) for h in input().strip().split()] stacks.append(stack[::-1]) goal = 1 best_height = 0 heights = [0] * num_stacks positions = [0] * num_stacks total_blocks = sum(num_blocks) while sum(positions) < total_blocks: for i, stack in enumerate(stacks): if positions[i] == num_blocks[i]: continue if heights[i] == goal: heights[i] += stack[positions[i]] positions[i] += 1 at_end = False while heights[i] < goal: if positions[i] == num_blocks[i]: at_end = True break heights[i] += stack[positions[i]] positions[i] += 1 goal = heights[i] if at_end: continue if check_equal_heights(heights): best_height = heights[i] print(best_height) Problem solution in Java Programming. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); int[] sum = new int[3]; int[] sn = new int[3]; for (int i =0; i< 3 ; i++) { sn[i] = in.nextInt(); } for(int i =0; i < 3; i++) { map.put(i, new ArrayList<Integer>()); for(int j=0 ; j < sn[i]; j++) { int k = in.nextInt(); sum[i] += k; map.get(i).add(0,k); } } while (!((sum[0] == sum[1]) && (sum[1] == sum[2]))) { int minSum = Math.max(Math.max(sum[0], sum[1]), sum[2]); int j = 0; if (minSum == sum[0]) j =0; else if (minSum == sum[1]) j = 1; else j = 2; sum[j] -= map.get(j).get(map.get(j).size() - 1); map.get(j).remove(map.get(j).size() - 1); } System.out.println(sum[0]); } } Problem solution in C++ programming. #include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll; template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; } template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; } int main() { int n1; int n2; int n3; while(~scanf("%d%d%d", &n1, &n2, &n3)) { vector<int> a(n1); for(int i = 0; i < n1; ++ i) scanf("%d", &a[i]); vector<int> b(n2); for(int i = 0; i < n2; ++ i) scanf("%d", &b[i]); vector<int> c(n3); for(int i = 0; i < n3; ++ i) scanf("%d", &c[i]); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); reverse(c.begin(), c.end()); map<int, int> t; rep(k, 3) { const vi &v = k == 0 ? a : k == 1 ? b : c; int sum = 0; for(int x : v) { sum += x; ++ t[sum]; } } int ans = 0; for(auto p : t) if(p.second == 3) amax(ans, p.first); printf("%dn", ans); } return 0; } Problem solution in C programming. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int findmax(int h1, int h2, int h3){ int max; if(h1 == h2 && h2 == h3) return h1; max = (h1 > h2) ? 1:2; if(max == 1) max =(h1 > h3) ? 1:3; else max = (h2 > h3) ? 2:3; return max; } int balance(int *N1, int * N2, int *N3, int h1, int h2, int h3, int n1, int n2, int n3){ int k1, k2, k3; k1 =k2=k3=0; int max = findmax(h1,h2, h3); while(!(h1 == h2 && h2 == h3)){ max=findmax(h1, h2, h3); if(max == 1) h1 -= N1[k1++]; else if(max == 2) h2 -= N2[k2++]; else if(max == 3) h3 -= N3[k3++]; // printf("1:%d, 2:%d, 3:%dn",h1, h2, h3); if(h1 == 0 || h2 == 0 || h3 == 0) return 0; } return h2; } int main() { int n1,n2, n3, h1, h2, h3,i; h1=h2=h3=0; scanf("%d %d %d",&n1, &n2, &n3); int *N1=malloc(sizeof(int *)*n1); int *N2=malloc(sizeof(int *)*n2); int *N3=malloc(sizeof(int *)*n3); for(i=0; i<n1; i++){ scanf("%d", &N1[i]); h1 += N1[i]; } for(i=0; i<n2; i++){ scanf("%d", &N2[i]); h2 += N2[i]; } for(i=0; i<n3; i++){ scanf("%d", &N3[i]); h3 += N3[i]; } printf("%dn", balance(N1, N2, N3, h1, h2, h3, n1, n2,n3)); return 0; } Problem solution in JavaScript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n1_temp = readLine().split(' '); var n1 = parseInt(n1_temp[0]); var n2 = parseInt(n1_temp[1]); var n3 = parseInt(n1_temp[2]); h1 = readLine().split(' '); h1 = h1.map(Number); h2 = readLine().split(' '); h2 = h2.map(Number); h3 = readLine().split(' '); h3 = h3.map(Number); /////////// var arrStack = [h1, h2, h3]; var arrTopPointer = [0, 0, 0]; var arrSum = [getHeight(h1), getHeight(h2), getHeight(h3)]; var index = getMaxStackIndex(arrSum); while (index >= 0) { arrSum[index] -= arrStack[index][arrTopPointer[index]]; arrTopPointer[index]++; index = getMaxStackIndex(arrSum); } console.log(arrSum[0]); } function getMaxStackIndex(arrSum) { var hasMax = false; var maxVal = arrSum[0]; var maxIndex = 0; for (var i = 1; i < arrSum.length; i++) { if (arrSum[i] > maxVal) { hasMax = true; maxVal = arrSum[i]; maxIndex = i; } else if ((arrSum[i] < maxVal)) { hasMax = true; } } if (hasMax) { return maxIndex; } else { return -1; } } function getHeight(stack) { var sum = 0; for (var i = 0; i < stack.length; i++) { sum += stack[i]; } return sum; } coding problems data structure