Equal Stacks

  • + 0 comments

    Here is the approch for Time complexity O(n) and space complexity O(1).

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        // Write your code here
            long sum1 = 0;
            for(Integer h: h1)
                sum1 += h;
                
            long sum2 = 0;
            for(Integer h: h2)
                sum2 += h;
                
            long sum3 = 0;
            for(Integer h: h3)
                sum3 += h;
                
            //   H1    H2   H3
            int i = 0, j=0, k=0;
             
            // System.out.println("i "+ i + " j " +j +" k "+ k); 
            // System.out.println("h1 "+ sum1 + " h2 " +sum2 +" h3 "+ sum3); 
            while((i < h1.size() && j < h2.size() && k < h3.size()) 
                && (sum1 != sum2 || sum2 != sum3 || sum1 != sum3)) {
                
                if(sum1 > sum2) { // sum1
                    if(sum1 > sum3) { // sum1
                        sum1 -= h1.get(i);
                        i++;
                    } else if(sum3 > sum1) { //sum3
                        sum3 -= h3.get(k);
                        k++;
                    }else { //sum3 and sum1
                        sum1 -= h1.get(i);
                        i++;
                        sum3 -= h3.get(k);
                        k++;
                    }
                } else if(sum2 > sum1) { // sum2
                    if(sum2 > sum3) { // sum2
                        sum2 -= h2.get(j);
                        j++;
                    } else if(sum3 > sum2) { //sum3
                        sum3 -= h3.get(k);
                        k++;
                    }else { //sum2 and sum1
                        sum2 -= h2.get(j);
                        j++;
                        sum3 -= h3.get(k);
                        k++;
                    }
                }  
                else if( sum2 > sum3 ) { //sum2
                    if(sum2 > sum1) { // sum2
                        sum2 -= h2.get(j);
                        j++;
                    } else if(sum1 > sum2) { //sum1
                        sum1 -= h1.get(i);
                        i++;
                    }else { //sum2 and sum1
                        sum2 -= h2.get(j);
                        j++;
                        sum1 -= h1.get(i);
                        i++;
                    }
                } else if( sum3 > sum2) { // sum3
                     if(sum3 > sum1) { // sum3
                        sum3 -= h3.get(k);
                        k++;
                    } else if(sum1 > sum3) { //sum1
                        sum1 -= h1.get(i);
                        i++;
                    }else { //sum2 and sum1
                        sum3 -= h3.get(k);
                        k++;
                        sum1 -= h1.get(i);
                        i++;
                    }
                } else if(sum1 > sum3) { // sum1
                    if(sum1 > sum2) { // sum1
                        sum1 -= h1.get(i);
                        i++;
                    } else if(sum2 > sum1) { //sum3
                        sum2 -= h2.get(j);
                        j++;
                    }else { //sum3 and sum1
                        sum1 -= h1.get(i);
                        i++;
                        sum2 -= h2.get(j);
                        j++;
                    }
                } else if(sum3 > sum1) { // sum3
                    if(sum3 > sum2) { // sum3
                        sum3 -= h3.get(k);
                        k++;
                    } else if(sum2 > sum3) { //sum2
                        sum2 -= h2.get(j);
                        j++;
                    }else { //sum2 and sum3
                        sum3 -= h3.get(k);
                        k++;
                        sum2 -= h2.get(j);
                        j++;
                    }
                } 
                // System.out.println("i "+ i + " j " +j +" k "+ k); 
                // System.out.println("h1 "+ sum1 + " h2 " +sum2 +" h3 "+ sum3); 
            }
            if(sum1 == sum2 && sum2 == sum3)
                return (int)sum1;
            return 0;
        }