Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: can someone solve this problem

1. ## can someone solve this problem

Consider the sequence of digits from 1 through N (N<=9) in increasing order:
1 2 3 4 … N
Insert either a ‘+’ (for addition) or a ‘-‘ (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7
1+2-3+4-5-6+7=0
1+2-3-4+5+6-7=0
1-2+3+4-5+6-7=0
1-2-3-4-5+6+7=0

2. ## Re: can someone solve this problem

What's preventing you from coding it yourself? No one here will do it for you, but we'll do our best to help you with your code. Post some, and ask specific questions about it if you're having trouble.

3. ## The Following User Says Thank You to GregBrannon For This Useful Post:

shilpy (September 3rd, 2014)

4. ## Re: can someone solve this problem

i've tried a lot.....!!!!

5. ## Re: can someone solve this problem

Do you have an algorithm for solving the problem? Maybe that needs some work before you try writing anymore code.

6. ## Re: can someone solve this problem

basically m unable to decode it ......we've to take combinations of all the numbers...that's fine but what to do with + or - signs arrangement.

7. ## Re: can someone solve this problem

Without an algorithm its not possible to write any code.
To start on an algorithm, take a short sequence like: 1,2,3 and make a list of all the possible expressions that work. Then move to the next longer sequence and do it again.
What are the basic constraints?
There must be at least 1 -
The sum of the number of + and the number of - must equal the number of digits - 1.

8. ## The Following User Says Thank You to Norm For This Useful Post:

shilpy (September 3rd, 2014)

9. ## Re: can someone solve this problem

but how to put signs between them....how to decide....i am really very confused..!!!

10. ## Re: can someone solve this problem

how to put signs between them
For the String to disply, that will be done with String concatenation: "1" + "+" + "2" + "-" ....
The algorithm needs to determine where the "+" and "-" go.

When computing the sum, use -1 and +1 values:
To change 4 to -4, multiply it by -1 => 4 * -1 = -4
To leave 4 at 4, multiply it by +1 => 4 * +1 = 4

Use the +1 and -1 values in various combinations to build the sum of the digits.

11. ## The Following User Says Thank You to Norm For This Useful Post:

shilpy (September 3rd, 2014)

12. ## Re: can someone solve this problem

.....+ and - signs will also have permutation and combination sort of loop...to decide their positions..???

13. ## Re: can someone solve this problem

Use arrays of +1 and -1 values to determine the signs to use.
When building the String use "+" with a +1 value and "-" with the -1 value.

14. ## The Following User Says Thank You to Norm For This Useful Post:

shilpy (September 3rd, 2014)

15. ## Re: can someone solve this problem

...not to print in string...but for calculation part ...+ and - will be arranged in what manner....any sequence is used..?? please help me.. :/

16. ## Re: can someone solve this problem

take a piece of paper and draw out all the possible patterns for some short sequences.
Then find the logic to create all those patterns.

For example with 3 digits the patterns might be:
-+
--
+-

17. ## The Following User Says Thank You to Norm For This Useful Post:

shilpy (September 4th, 2014)

18. ## Re: can someone solve this problem

It may appear more familiar to you if you consider the possible permutations as binary patterns. In Norm's example, if - = 1 and + = 0, then the equivalent 0/1 binary patterns are:

10
11
01

and the missing one:

00

Good luck!

Oops! Fixed my mixup.

19. ## The Following User Says Thank You to GregBrannon For This Useful Post:

shilpy (September 4th, 2014)

20. ## Re: can someone solve this problem

....so if i am getting right then i have to apply combinations in signs and a simple loop for numbers till the range entered ...!!! then i need not to apply "4* -1 = -4 " for making it negative...or positive...!!!

--- Update ---

...i think now i can clearly solve it....!!! thanx sir...