Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:26:52+00:00 2026-06-10T11:26:52+00:00

Given an array A, suppose {5, 5, 4, 3, 7} , use operation {*,

  • 0

Given an array A, suppose {5, 5, 4, 3, 7}, use operation {*, /, + -} on 5, 5, 4, 3 and get the result as 7. If it is possible, print the answer. If it is not, return 0.

Answer : 5 * 5 - 4 / 3 = 7.

I solved this using recursion, brute force, checking all the cases.

My code :

void check_result(int* arr, int size, char* op, int depth, int result, int val)
{
    if(depth==size)
    {
        if(val==result)
        {
             // call function to print pattern of op[]
        }
        return ;
    }
    else
    {
        op[depth]='+';
        check_result( arr+1, size, op, depth+1, result, val + *arr );

        op[depth]='-';
        check_result( arr+1, size, op, depth+1, result, val - *arr );

        op[depth]='*';
        check_result( arr+1, size, op, depth+1, result, val * (*arr) );

        op[depth]='/';
        if( *arr )
            check_result( arr+1, size, op, depth+1, result, val / (*arr) );
    }
}

Can you tell me dynamic approach of this question, because this takes O(4^n) time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:26:54+00:00Added an answer on June 10, 2026 at 11:26 am

    The state should be DP[position][currentValue]. It means if that the neededResult can be achieved if you are at the corresponding position and having currentValue.

    boolean rec(int position, int currentValue)
    {
       if (position == lastPosition)
       {
          return currentValue == neededResult;
       } 
    
       if (computed[position][currentValue]) return DP[position][currentValue]; 
    
       boolean can = false; 
       int nextNumber = numbers[position + 1];
    
       //Try every operation here and call rec again
       can |= rec(position + 1, currentValue + nextNumber);
       can |= rec(position + 1, currentValue - nextNumber);
       can |= rec(position + 1, currentValue * nextNumber);
       can |= rec(position + 1, currentValue / nextNumber);
    
       DP[position][currentValue] = can;
       computed[position][currentValue] = true;
       return can;
    }
    

    You can then restore the result in another method:

       void restore(int position, int currentValue)
        {
           if (position == lastPosition) return;
    
           if (DP[position + 1][currentValue + nextNumber]) 
           { 
              cout << " + ";          
              restore(position + 1, currentValue + nextNumber);
           }
           else
           if (DP[position + 1][currentValue - nextNumber]) 
           { 
              cout << " - ";          
              restore(position + 1, currentValue - nextNumber);
           }
           else
           if (DP[position + 1][currentValue * nextNumber]) 
           { 
              cout << " * ";          
              restore(position + 1, currentValue * nextNumber);
           }
           else
           if (DP[position + 1][currentValue / nextNumber]) 
           { 
              cout << " / ";          
              restore(position + 1, currentValue / nextNumber);
           }
        }
    

    P.S. This is only PSEUDOCODE. There may be errors and missed corner cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array like this: Array => ( [0] => 1, [1] => 2,
I need to use (not implement) an array based version of Dijkstras algo .The
Suppose you pass an array as argument to a given method, say, public static
Suppose I make a 2d array like this: >>> A=np.arange(16).reshape((4,4)) >>> A array([[ 0,
We are suppose to write a calendar for a project. I'm using an array
Codingbat.com array question This is a simple array question, not for homework, just for
Suppose I am given a following text (in a string array) engine.STEPCONTROL(00000000,02000001,02000043,02000002,02000007,02000003,02000008,02000004,02000009,02000005,02000010,02000006,02000011); if(02000001 ==
So the situation is like this. Suppose we have: class Test(datetime): def a(self): return
given an array: array = [16 16 16 22 23 23 23 25 52

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.