Sort by

recency

|

1134 Discussions

|

  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'equalStacks' function below.

    #

    The function is expected to return an INTEGER.

    The function accepts following parameters:

    1. INTEGER_ARRAY h1

    2. INTEGER_ARRAY h2

    3. INTEGER_ARRAY h3

    #

    def equalStacks(h1, h2, h3): sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3) i, j, k = 0, 0, 0

    while not (sum1 == sum2 == sum3):  
        if sum1 > sum2 or sum1 > sum3:
            sum1 -= h1[i]
            i += 1
        elif sum2 > sum1 or sum2 > sum3:
            sum2 -= h2[j]
            j += 1
        elif sum3 > sum1 or sum3 > sum2:
            sum3 -= h3[k]
            k += 1
        if i == len(h1) or j == len(h2) or k == len(h3):
            return 0
    return sum1
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()
    
    n1 = int(first_multiple_input[0])
    
    n2 = int(first_multiple_input[1])
    
    n3 = int(first_multiple_input[2])
    
    h1 = list(map(int, input().rstrip().split()))
    
    h2 = list(map(int, input().rstrip().split()))
    
    h3 = list(map(int, input().rstrip().split()))
    
    result = equalStacks(h1, h2, h3)
    
    fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 1 comment
    def equalStacks(h1, h2, h3):
        sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3)
        i, j, k = 0, 0, 0
        
        while not (sum1 == sum2 == sum3):  
            if sum1 > sum2 or sum1 > sum3:
                sum1 -= h1[i]
                i += 1
            elif sum2 > sum1 or sum2 > sum3:
                sum2 -= h2[j]
                j += 1
            elif sum3 > sum1 or sum3 > sum2:
                sum3 -= h3[k]
                k += 1
            if i == len(h1) or j == len(h2) or k == len(h3):
                return 0
        return sum1
    

    or

    def equalStacks(h1, h2, h3):
        sum_h1, sum_h2, sum_h3 = sum(h1), sum(h2), sum(h3)
        
        while sum_h1 != sum_h2 or sum_h2 != sum_h3:
            if sum_h1 > sum_h2 or sum_h1 > sum_h3:
                sum_h1 -= h1.pop(0)
            elif sum_h2 > sum_h3:
                sum_h2 -= h2.pop(0)
            else:
                sum_h3 -= h3.pop(0)
        
        return sum_h1
    
  • + 0 comments

    using javascript

    function equalStacks(h1, h2, h3) {
      let size1 = 0,
        size2 = 0,
        size3 = 0,
        maxSize = 0;
      while (true) {
        if (size1 === size2 && size2 === size3) maxSize = size1;
        if (!h1.length && !h2.length && !h3.length) {
          break;
        }
        
        if (size1 <= size2 && size1 <= size3 && h1.length) {
          size1 += h1.pop();
        } else if (size2 <= size1 && size2 <= size3 && h2.length) {
          size2 += h2.pop();
        } else if (size3 <= size1 && size3 <= size2 && h3.length) {
          size3 += h3.pop();
        }else break
      }
    
      return maxSize;
    }
    
  • + 0 comments

    Here is my C++ solution:

    int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3) {
    
        auto ref = [&](){
            std::vector<vector<int>*> v_;
            v_.push_back(&h1);
            v_.push_back(&h2);
            v_.push_back(&h3);
            return v_;
        }();
    
        int hight[3]{};
        for (auto it : h1) hight[0] += it;
        for (auto it : h2) hight[1] += it;
        for (auto it : h3) hight[2] += it;
    
        while (true) {
            if (hight[0] == hight[1] && hight[0] == hight[2]) return hight[0];
    
            int indx = 0;
            if (hight[1] > hight[indx]) indx = 1;
            else if (hight[2] > hight[indx]) indx = 2;
            
            auto el = ref[indx]->front();
            ref[indx]->erase(ref[indx]->begin());
            hight[indx] -= el;
        }
    
        return hight[0];
    }
    
  • + 0 comments

    public static int highest(int s1, int s2, int s3){ return Math.max(s1,Math.max(s2,s3)); }

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
    // Write your code here
    int n1=h1.size();
    int n2=h2.size();
    int n3=h3.size();
    int s1=0;
    int s2=0;
    int s3=0;
    int high;
    int p1=0; 
    int p2=0;
    int p3=0;
    
    for(int i=0;i<n1;i++){
        s1+=h1.get(i);
    }
    for(int i=0;i<n2;i++){
        s2+=h2.get(i);
    }
    for(int i=0;i<n3;i++){
        s3+=h3.get(i);
    }
    
    while(p1<h1.size() && p2<h2.size() && p3<h3.size()){
    
        if(s1==s2 && s2==s3){
            return s1;
        }
    
        else{
            high=highest(s1,s2,s3);
    
            if(high==s1){
                s1-=h1.get(p1);
                p1++;
            }
            if(high==s2){
                s2-=h2.get(p2);
                p2++;
            }
            if(high==s3){
                s3-=h3.get(p3);
                p3++;
            }
        }
    }
    
    return 0;
    

    }

    }