Arithmetic Expressions

Sort by

recency

|

55 Discussions

|

  • + 0 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;
    }
    
  • + 0 comments

    Simplest Solution I have Seen So far:-

    Code

    class Result {
    
    public static String ans="";
    
    public static boolean Solve(List<Integer>arr,int prev,String cur,int idx,boolean map[]  []){
            prev=prev%101;
    
     if(idx==arr.size()){
            if(prev%101==0){
                     ans=cur;
                        return true;  
                 }
    return false;
    }
    
     //Checking if at this index Previsiously we Got this remainder(prev)
    
    if(map[idx][prev+101])return false;
    map[idx][prev+101]=true;
    
        //recursion   
    
        if(Solve(arr,prev+arr.get(idx)%101,cur+"+"+arr.get(idx),idx+1,map))return true;
        if(Solve(arr,prev-arr.get(idx)%101,cur+"-"+arr.get(idx),idx+1,map))return true;
        if(Solve(arr,prev*arr.get(idx)%101,cur+"*"+arr.get(idx),idx+1,map))return true;
    
       return false;
    }
    
    public static String arithmeticExpressions(List<Integer> arr){
    
    boolean[][]map=new boolean[arr.size()][2*101];
    
    Solve(arr,arr.get(0),""+arr.get(0),1,map);
    
     return ans;
    }
    

    }

  • + 0 comments

    Despite this problem being in the category "Recursion", I feel it much more appropriate to use DP.

    Here what that looks like in Python3:

    def arithmeticExpressions(arr):
        this = [None] * 101
        this[arr[0]] = [arr[0]]
        for a in arr[1:]:
            if this[0]:
                this[0] += ['*', a]
            else:
                that = [None] * 101
                for i, v in enumerate(this):
                    if v is not None:
                        that[(i+a)%101] = this[i] + ['+', a]
                        that[(i-a)%101] = this[i] + ['-', a]
                        that[(i*a)%101] = this[i] + ['*', a]
                this = that
        return ''.join(map(str, this[0]))
    
  • + 0 comments

    Here is Arithmetic Expressions problem solution in Python Java C++ and C programming - https://programs.programmingoneonone.com/2021/07/hackerrank-arithmetic-expressions-problem-solution.html

  • + 0 comments

    Hi, Can someone tell what is the issue with this code.I get RunTimeError for 7th test case . Its not TLE Error

    def getexpression(arr,operators,curInd,curresult,expr):

    if curInd == len(arr):
             if curresult%101 ==0:
                 return([True,expr])
             else:
                 return([False,""])
    

    resultTillnow=curresult

    expTillnow=expr
    
      for op in operators:
        val2=arr[curInd]
        if op=="ADD":
            result=resultTillnow+val2
            oper="+"
        elif op=="MUL":
            result=resultTillnow*val2
            oper="*"
        else:
            result=resultTillnow-val2
            oper="-"
        nexpr=expTillnow+oper+str(val2)    
        stat,e=getexpression(arr,operators,curInd+1,result,nexpr)
        if stat:
            return([stat,e])
    return([False,""])                            
    

    def arithmeticExpressions(arr):

    operators=['ADD','MUL','SUB']
    
    stat,expr = getexpression(arr,operators,1,arr[0],str(arr[0]))  
    
        if stat:
    
        return expr
        tat:
        return(expr)
       return exprelse:
        return("")