You are viewing a single comment's thread. Return to all comments →
Can someone explain why is this code failing. It passes 24 test cases only
class Solver{ public: string main_result; bool completed = false; void compute(vector<int>arr,int sum,string result){ int cp_sum = sum; if(sum==0){ int n = arr.at(0); arr.erase(arr.begin()); compute(arr,n,result+to_string(n)); return; } if(arr.size() == 0){ if(sum%101==0){ main_result = result; completed = true; // cout<<main_result.length()<<endl; cout<<sum<<endl; return; } return; } for(int i = 1;i<=3 && completed == false;i++){ int n = arr.at(0); if(i == 1){ //sum+=n; arr.erase(arr.begin()); compute(arr,sum+n,result+ "+" + to_string(n)); } if(i == 2){ //sum-= n; arr.erase(arr.begin()); compute(arr,sum-n,result+ "-" + to_string(n)); } if(i == 3){ //sum*=n; arr.erase(arr.begin()); compute(arr,sum*n,result+ "*" + to_string(n)); } arr.insert(arr.begin(),n); // sum = cp_sum; } } }; string arithmeticExpressions(vector<int> arr) { Solver obj; obj.compute(arr, 0, ""); string ans = obj.main_result; return ans; }
Seems like cookies are disabled on this browser, please enable them to open this website
Arithmetic Expressions
You are viewing a single comment's thread. Return to all comments →
Can someone explain why is this code failing. It passes 24 test cases only