The three stacks s1, s2 and s3, each containing positive integers, are given. The task is to find the maximum possible equal sum that can be achieved by removing elements from the top of the stacks. Elements can be removed from the top of each stack, but the final sum of the remaining elements in all three stacks must be the same. The goal is to determine the maximum possible equal sum that can be achieved after removing elements.
Note: The stacks are represented as arrays, where the first index of the array corresponds to the top element of the stack.
Examples:Â
Input: s1 = [3, 2, 1, 1, 1], s2 = [4, 3, 2], s3 = [2, 5, 4, 1] Output: 5 Explanation: We can pop 2 elements from the 1st stack, 1 element from the 2nd stack and 2 elements from the 3rd stack.
Input: s1 = [3, 10] s2 = [4, 5] s3 = [2, 1] Output: 0 Explanation: Sum can only be equal after removing all elements from all stacks.
Greedy Approach - O(n1 + n2 + n3) Time and O(1) Space
The idea is to compare the sum of each stack, if they are not same, remove the top element of the stack having the maximum sum.
Algorithm for solving this problem:Â
Find the sum of all elements of in individual stacks.
If the sum of all three stacks is the same, then this is the maximum sum.
Else remove the top element of the stack having the maximum sum among three of stacks. Repeat step 1 and step 2.
The approach works because elements are positive. To make sum equal, we must remove some element from stack having more sum, and we can only remove from the top.
C++
#include<iostream>#include<vector>usingnamespacestd;intmaxSum(vector<int>&s1,vector<int>&s2,vector<int>&s3){intsum1=0,sum2=0,sum3=0;// Manually summing the elements of s1for(inti=0;i<s1.size();i++){sum1+=s1[i];}// Manually summing the elements of s2for(inti=0;i<s2.size();i++){sum2+=s2[i];}// Manually summing the elements of s3for(inti=0;i<s3.size();i++){sum3+=s3[i];}inttop1=0,top2=0,top3=0;while(true){if(top1==s1.size()||top2==s2.size()||top3==s3.size())return0;if(sum1==sum2&&sum2==sum3)returnsum1;if(sum1>=sum2&&sum1>=sum3)sum1-=s1[top1++];elseif(sum2>=sum1&&sum2>=sum3)sum2-=s2[top2++];elsesum3-=s3[top3++];}}intmain(){vector<int>s1={3,2,1,1,1};vector<int>s2={4,3,2};vector<int>s3={1,1,4,1};cout<<maxSum(s1,s2,s3)<<endl;return0;}
Java
importjava.util.List;importjava.util.ArrayList;publicclassGfG{publicstaticintmaxSum(List<Integer>s1,List<Integer>s2,List<Integer>s3){intsum1=0,sum2=0,sum3=0;// Manually summing the elements of s1for(inti=0;i<s1.size();i++){sum1+=s1.get(i);}// Manually summing the elements of s2for(inti=0;i<s2.size();i++){sum2+=s2.get(i);}// Manually summing the elements of s3for(inti=0;i<s3.size();i++){sum3+=s3.get(i);}inttop1=0,top2=0,top3=0;while(true){if(top1==s1.size()||top2==s2.size()||top3==s3.size())return0;if(sum1==sum2&&sum2==sum3)returnsum1;if(sum1>=sum2&&sum1>=sum3)sum1-=s1.get(top1++);elseif(sum2>=sum1&&sum2>=sum3)sum2-=s2.get(top2++);elsesum3-=s3.get(top3++);}}publicstaticvoidmain(String[]args){List<Integer>s1=newArrayList<>();s1.add(3);s1.add(2);s1.add(1);s1.add(1);s1.add(1);List<Integer>s2=newArrayList<>();s2.add(4);s2.add(3);s2.add(2);List<Integer>s3=newArrayList<>();s3.add(1);s3.add(1);s3.add(4);s3.add(1);System.out.println(maxSum(s1,s2,s3));}}
Suffix Sum and Hash Set - O(n1 + n2 + n3) Time and O(n1 + n2 + n3) Space
The problem can be reinterpreted as we need to find the maximum equal suffix sum of the three array.
As we remove element from the top of stack the remaining sum is the suffix sum up to current element. So, if we put all the suffix sums of stack1 and stack2 in an unordered set and traverse the suffix sum array of stack3, and if we find a suffix sum which is present in all three then it is our ans.
Calculate suffix sum of stack1 and stack2 and insert each element in unorderd_set1 and unordered_set2 respectively.
Calculate suffix sum of stack 3 and store in vector named as suffix.
Traverse the suffix vector from i = 0 to i = prefix.size() - 1
Find an index where the suffix sums of all the three stacks are equal
If not found return 0
C++
#include<iostream>#include<vector>#include<unordered_set>usingnamespacestd;intmaxSum(vector<int>&s1,vector<int>&s2,vector<int>&s3){vector<int>suffix(s3.size()+1,0);intsum1=0;unordered_set<int>st1;// Calculate suffix sum of s1 and store in unordered_set st1for(inti=s1.size()-1;i>=0;i--){sum1+=s1[i];st1.insert(sum1);}intsum2=0;unordered_set<int>st2;// Calculate suffix sum of s2 and store in unordered_set st2for(inti=s2.size()-1;i>=0;i--){sum2+=s2[i];st2.insert(sum2);}// Calculate suffix sum of s3 and store in suffix vectorfor(inti=s3.size()-1;i>=0;i--){suffix[i]=suffix[i+1]+s3[i];}// Find the maximum suffix sum present in all three stacksfor(inti=0;i<suffix.size();i++){if(st1.find(suffix[i])!=st1.end()&&st2.find(suffix[i])!=st2.end()){returnsuffix[i];}}return0;}intmain(){vector<int>s1={3,2,1,1,1};vector<int>s2={4,3,2};vector<int>s3={1,1,4,1};cout<<maxSum(s1,s2,s3)<<endl;return0;}
Java
importjava.util.HashSet;importjava.util.Set;publicclassGfG{publicstaticintmaxSum(int[]s1,int[]s2,int[]s3){int[]suffix=newint[s3.length+1];intsum1=0;Set<Integer>st1=newHashSet<>();// Calculate suffix sum of s1 and store in HashSet st1for(inti=s1.length-1;i>=0;i--){sum1+=s1[i];st1.add(sum1);}intsum2=0;Set<Integer>st2=newHashSet<>();// Calculate suffix sum of s2 and store in HashSet st2for(inti=s2.length-1;i>=0;i--){sum2+=s2[i];st2.add(sum2);}// Calculate suffix sum of s3 and store in suffix arrayfor(inti=s3.length-1;i>=0;i--){suffix[i]=suffix[i+1]+s3[i];}// Find the maximum suffix sum present in all three stacksfor(inti=0;i<suffix.length;i++){if(st1.contains(suffix[i])&&st2.contains(suffix[i])){returnsuffix[i];}}return0;}publicstaticvoidmain(String[]args){int[]s1={3,2,1,1,1};int[]s2={4,3,2};int[]s3={1,1,4,1};System.out.println(maxSum(s1,s2,s3));}}
Python
defmaxSum(s1,s2,s3):suffix=[0]*(len(s3)+1)sum1=0st1=set()# Calculate suffix sum of s1 and store in set st1foriinrange(len(s1)-1,-1,-1):sum1+=s1[i]st1.add(sum1)sum2=0st2=set()# Calculate suffix sum of s2 and store in set st2foriinrange(len(s2)-1,-1,-1):sum2+=s2[i]st2.add(sum2)# Calculate suffix sum of s3 and store in suffix listforiinrange(len(s3)-1,-1,-1):suffix[i]=suffix[i+1]+s3[i]# Find the maximum suffix sum present in all three stacksforiinrange(len(suffix)):ifsuffix[i]inst1andsuffix[i]inst2:returnsuffix[i]return0s1=[3,2,1,1,1]s2=[4,3,2]s3=[1,1,4,1]print(maxSum(s1,s2,s3))
C#
usingSystem;usingSystem.Collections.Generic;classProgram{publicstaticintMaxSum(int[]s1,int[]s2,int[]s3){int[]suffix=newint[s3.Length+1];intsum1=0;HashSet<int>st1=newHashSet<int>();// Calculate suffix sum of s1 and store in HashSet st1for(inti=s1.Length-1;i>=0;i--){sum1+=s1[i];st1.Add(sum1);}intsum2=0;HashSet<int>st2=newHashSet<int>();// Calculate suffix sum of s2 and store in HashSet st2for(inti=s2.Length-1;i>=0;i--){sum2+=s2[i];st2.Add(sum2);}// Calculate suffix sum of s3 and store in suffix arrayfor(inti=s3.Length-1;i>=0;i--){suffix[i]=suffix[i+1]+s3[i];}// Find the maximum suffix sum present in all three stacksfor(inti=0;i<suffix.Length;i++){if(st1.Contains(suffix[i])&&st2.Contains(suffix[i])){returnsuffix[i];}}return0;}staticvoidMain(){int[]s1={3,2,1,1,1};int[]s2={4,3,2};int[]s3={1,1,4,1};Console.WriteLine(MaxSum(s1,s2,s3));}}
JavaScript
functionmaxSum(s1,s2,s3){constsuffix=newArray(s3.length+1).fill(0);letsum1=0;constst1=newSet();// Calculate suffix sum of s1 and store in Set st1for(leti=s1.length-1;i>=0;i--){sum1+=s1[i];st1.add(sum1);}letsum2=0;constst2=newSet();// Calculate suffix sum of s2 and store in Set st2for(leti=s2.length-1;i>=0;i--){sum2+=s2[i];st2.add(sum2);}// Calculate suffix sum of s3 and store in suffix arrayfor(leti=s3.length-1;i>=0;i--){suffix[i]=suffix[i+1]+s3[i];}// Find the maximum suffix sum present in all three stacksfor(leti=0;i<suffix.length;i++){if(st1.has(suffix[i])&&st2.has(suffix[i])){returnsuffix[i];}}return0;}consts1=[3,2,1,1,1];consts2=[4,3,2];consts3=[1,1,4,1];console.log(maxSum(s1,s2,s3));