You are viewing a single comment's thread. Return to all comments →
This code is getting time out for most of the cases. I think it is complexity O(m+n+o)(worst case) m,n,o being length of stack.
Are we supposed to do better than this ?
int main(){ int n1, n2, n3; unsigned long long int sum1 = 0, sum2 = 0, sum3 = 0; int i1 = 0,i2 = 0,i3 = 0; cin >> n1 >> n2 >> n3; vector<int> h1(n1); for(int h1_i = 0;h1_i < n1;h1_i++){ cin >> h1[h1_i]; sum1 += h1[h1_i]; } vector<int> h2(n2); for(int h2_i = 0;h2_i < n2;h2_i++){ cin >> h2[h2_i]; sum2 += h2[h2_i]; } vector<int> h3(n3); for(int h3_i = 0;h3_i < n3;h3_i++){ cin >> h3[h3_i]; sum3 += h3[h3_i]; } while (i1<n1 && i2<n2 && i3<n3) { if (sum1 == sum2 && sum2 == sum3) { break; } else if (sum1 > sum2 && sum1 > sum3) { sum1 = sum1 - h1[i1]; i1++; } else if (sum2 > sum1 && sum2 > sum3) { sum2 = sum2 - h2[i2]; i2++; } else if (sum3 > sum1 && sum3 > sum2) { sum3 = sum3 - h3[i3]; i3++; } } if (sum1 == sum2 && sum2 == sum3) cout<<sum1; else cout<<0; return 0; }
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 →
This code is getting time out for most of the cases. I think it is complexity O(m+n+o)(worst case) m,n,o being length of stack.
Are we supposed to do better than this ?