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

The Archive Base Latest Questions

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

The question Consider the string of digits 123456789 . Consider all arithmetic expression that

  • 0

The question

Consider the string of digits 123456789. Consider all arithmetic expression that can be formed by placing + or - interspersed within the string. Examples:

1 + 2 - 345 + 67 - 8 - 9 = 292
123456 - 789 = 122667

Write a Java program that uses a stack to find such a combination that has value 2012.

My Problem

I am stuck with the logic since we have to use two arithmetic operators.

import java.util.*;

public class arithmeticStack {
    public static void main (String args[]) {
        ArrayList<String> dg = new ArrayList<String>();
        Stack<String> digits = new Stack<String>();
        int number = 0;
        dg.add("1");
        dg.add("2");
        dg.add("3");
        dg.add("4");
        dg.add("5");
        dg.add("6");
        dg.add("7");
        dg.add("8");
        dg.add("9");

        for (int i = 0; i <= dg.size() - 1; i++) {
            digits.push(dg.get(i));
        }

        for (String f : digits){
            number += Integer.parseInt(f);
        }

        while (number == 2012) {
        }                                     
    }
}
  • 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-13T15:45:46+00:00Added an answer on June 13, 2026 at 3:45 pm

    Rough idea, this is what I can think:

    Think that there are 8 empty boxes between the numbers, which can be either filled by a +,- or nothing. So you can get 3^8 = 6561 different permutations. Create an array of 8 chars. Now permute this array and find all its possibilities.. sorry I don’t know how to do this right now, maybe someone else can explain it to you, I’m pretty sure it can be done. Use these permutations to insert these values in the original string.

    You can use a stack to evaluate an expression. Can be done without considering operator precedence in your case roughly like this:

    Iterate from left to right, if you encounter an operand, push it to the stack. If you encounter an operator, push it to the stack again. Now when you enter a second operand, pop and retrieve the operator, pop again to retrieve an operand, do the operation with these two operands and the operator, and push the result back to the stack. In the end, you have the result on the stack.

    Edit: Or just use Python’s eval…

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

Sidebar

Related Questions

I am preparing for my interview and came across this question: Consider that i
Consider an arbitrary text box that records the answer to the question, what do
Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
Original Question Consider the following scenario: public abstract class Foo { public string Name
I have a rather basic C++ question: Consider a function that takes some input
Consider the need for a function in C# that will test whether a string
Consider an algorithm that needs to determine if a string contains any characters outside
Main Question : Ok guys, here is the situation, let's consider 2 string arrays
This question is about altering how the Grails data-binding handles string-to-integer conversion. Consider the
The question is asked with respect to an Object DataSource. So Consider that I

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.