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 8077999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:39:13+00:00 2026-06-05T15:39:13+00:00

Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^

  • 0

Problem
Given a boolean expression consisting of the symbols 0, 1, &, |, ^ and a desired boolean result value, implement a function to count the number of ways of parenthesizing the expression such that it evaluates to result.
Example
Expression 1^0|0|1
Desired Result 0
Output 2, 1^((0|0)|1), 1^(0|(0|1))

My idea is to use backtracking, and evaluate an expression of the form a operator b. For example
1^0|0|1
-------
0123456

There are 3 possible evaluations: 0, 2, 4, more specifically, I have:
(1) evaluate at 0 -> 1|0|1
(2) evaluate at 0 -> 1|1
(3) evaluate at 0 -> 1

Then I backtrack at (2), to evaluate at position 2… The idea is very simple, but it produced duplicate result. The number of ways for result = 1 should be 3 but my approach yields 4.

bool evaluate(const string& expr) {
    assert(expr.length() == 3);
    assert(expr[0] == '0' || expr[0] == '1');
    assert(expr[1] == '^' || expr[1] == '|' || expr[1] == '&');
    assert(expr[2] == '0' || expr[2] == '1');

    bool result;
    bool a = (expr[0] == '1' ? 1 : 0);
    bool b = (expr[2] == '1' ? 1 : 0);

    switch (expr[1]) {
        case '^' :
            result = a ^ b;
            break;

        case '|' :
            result = a | b;
            break;

        case '&' :
            result = a & b;
            break;
    }

    return result;
}

void transform_at(string& s, int start) {
    bool result = evaluate(s.substr(start, 3));
    string left = s.substr(0, start);
    string right = s.substr(start + 3);
    result ? left.append(1, '1') : left.append(1, '0');
    s = left + right;
}

int count_parenthese_grouping(string expr, const bool result) {
    cout << "[recurse on]: " << expr << endl;
    if (expr.length() == 3 && evaluate(expr) == result) {
        return 1;
    }
    else if (expr.length() == 3 && evaluate(expr) != result) {
        return 0;
    }
    else {
        int operators = expr.length() - 2;
        int total = 0;

        for (int i = 0; i < operators; i += 2) {
            string temp = expr;
            transform_at(expr, i);
            total += count_parenthese_grouping(expr, result);
            expr = temp;
        }

        return total;
    }
}

I couldn’t see how this solution generated duplicate result! Could anyone help me out?

  • 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-05T15:39:14+00:00Added an answer on June 5, 2026 at 3:39 pm

    The duplication comes from the fact that you can arrive at (1^0)|(0|0) in two ways: first, parenthesize 1^0 then 0|0; second, parenthesize 0|0 then 1^0.

    You need to ensure that you only count the same parenthesizing once.

    A possible approach is to calculate an identifing number from the parenthesizing, then maintain a set of these id numbers and only count the ones that were not in the set yet.

    One possibility for such id would be to represent the parentheses in a bit pattern: the first n-1 bits representing first-level parhentheses, the next n-2 bits representing second-level parentheses (parentheses containing first-level ones), etc.

    so for example

    (1^0)|0|0   would become 10000
    1^(0|0)|0   would become 01000
    1^0|(0|0)   would become 00100
    (1^0)|(0|0) would become 10100
    (1^(0|0))|0 would become 01010
    1^((0|0)|0) would become 01001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the problem how to change boolean value of a given class so
The problem is : Given a number of programming libraries with similar or equal
I have dynamic number of checkboxes in jsp page as given below. <s:iterator value=details
I'm trying to generate a truth table for a given boolean expression. I could
Problem Given the following two tables, I'd like to select all Ids for Posts
First the practical application that led me to the problem: Given a set of
Please help me out with an algorithm for the following problem - Given a
My given problem is follow: I have an object with x columns and every
I've come across a tuples problem where given a list of pair tuples it
I have a homework question that says: Problem 1: Given the array [ 22

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.