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

  • SEARCH
  • Home
  • 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 4024352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:42:40+00:00 2026-05-20T10:42:40+00:00

I am trying to solve one of the classic dynamic programming problems. The problem

  • 0

I am trying to solve one of the “classic” dynamic programming problems. The problem is – Given a number as input , generate possible nested conditions.

Edit: as pointed out by temp below , i am going to first try and sort this out using recursion and then try with dynamic programming.

ie. if n = 3

O/p
((()))
()()()
(())()
()(())
(()())

My approach to the problem is based on two rules.

  1. Add a “(” brace if max number (
    here 3 ) is not reached and number
    of left braces is less than or equal
    to number of right braces.
  2. Add a right brace only if max number
    is
    not reached and number of right
    braces is less than number of left
    braces.

In theory they sound right ,but it falls face flat on the below source. Please excuse the hard-coding.

Edit : I have made some modifications and moved an inch closer to the solution 🙂

 #include <iostream>
#include <vector>
#include <string>

using namespace std;

void printPar(int l,int r,string s)
{
    if(l > 3 || r > 3 || r >l)
        return;



    if(l==3 && r==3)
    {
        cout<<s<<endl;
        return;
    }
    else
    {

         if((l<3))
         {
            s+="<";
            l = l+1;
            printPar(l,r,s);
         }
          if(r<3 && r < l)
         {
             s+=">";
             r = r+1;
            printPar(l,r,s);
         }
       //  cout<<"Exiting "<<l<<" & "<<r<<" "<<s<<endl;
    }
}




int main()
{
    string s;
    printPar(0,0,s);
    return 0;
}

Debug:

<<<>>>
<<<>>>
<<><>>
<<><>>
<><<>>
<><<>>
<><><>
<><><>

I understand why there are duplicate values in the list. i.e once the function is called using recursion and ends up following the next branch on execution. The second print is due to the function it self falling on the second branch. Is there any way to handle this ? I really do not want to go the global-set route.

Also , in my head this code should print (())() – Yet it does not 🙁

Can some one please point out the error ?

Thanks!

I know the condition needs some tweaking , but i have been staring at this endlessly. Halp!

  • 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-05-20T10:42:41+00:00Added an answer on May 20, 2026 at 10:42 am

    Right now, your solution doesn’t take advantage of dynamic programming. If you want to use DP here, you’ll need to think about the problem recursively. Fortunately, there’s a great recursive formulation to this problem:

    1. There is only one way to make balanced parentheses from no parentheses, which is to have no parentheses at all.
    2. If you have n + 1 pairs of parentheses to balance, then you can generate all balanced parentheses as follows: for all i from 0 to n, construct every string of the form (X)Y, where X is a string of i balanced parentheses and Y is a string of n – i balanced parentheses.

    The beauty of this setup is that to compute all strings of n + 1 balanced parentheses, you only need to know how to make balanced parentheses for 0, 1, 2, …, n + 1. Consequently, you can solve this problem by iterative constructing solutions for n = 0, 1, 2, …, etc. and reusing the results you produced at earlier steps.

    Hope this helps!

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

Sidebar

Related Questions

No related questions found

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.