473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Zero Sum

Hi everyone,

I need help for my project in my Java Programming 1 subject. I really don't have an idea on how to go about with this problem. I have started building the code for the CLASS, but didn't bother to post it, cause everytime after I declare the main method, my mind freezes, that's why I decided to ask for your help. This is the problem anyway...

Consider the sequence of digits from 1 through N (where N = 9) in increasing order 1 2 3 4 5 . . . .N, and insert a (+) for addition or a (-) for subtraction or a () [blank] to run the digits together. Now sum the result and see if you get zero.

Write a program that will find all sequences of length N that produces a ZERO SUM.

Test Case 1

Input
7

Output
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
1 - 23 + 4 + 5 + 6 + 7 = 0
1 - 23 + 45 + 67 = 0


Test Case 2

Input
8

Output
1 + 2 + 3 + 4 - 5 - 6 - 7 + 8 = 0
1 + 2 + 3 - 4 + 5 - 6 + 7 - 8 = 0
1 + 2 - 3 + 4 + 5 + 6 - 7 - 8 = 0
1 + 2 - 3 - 4 - 5 - 6 + 7 + 8 = 0
1 + 23 - 45 + 6 + 7 + 8 = 0
1 - 2 + 3 - 4 - 5 + 6 - 7 + 8 = 0
1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 = 0
1 - 2 - 3 + 4 - 5 + 6 + 7 - 8 = 0
1 - 23 - 4 + 5 + 6 + 7 + 8 = 0
12 - 34 - 56 + 78 = 0

You may test this program by entering the integer from the keyboard.


Thanks, your help is highly appreciated...
Mar 11 '07 #1
8 1968
sicarie
4,677 Expert Mod 4TB
Hi everyone,

I need help for my project in my Java Programming 1 subject. I really don't have an idea on how to go about with this problem. I have started building the code for the CLASS, but didn't bother to post it, cause everytime after I declare the main method, my mind freezes, that's why I decided to ask for your help. This is the problem anyway...

Consider the sequence of digits from 1 through N (where N = 9) in increasing order 1 2 3 4 5 . . . .N, and insert a (+) for addition or a (-) for subtraction or a () [blank] to run the digits together. Now sum the result and see if you get zero.

Write a program that will find all sequences of length N that produces a ZERO SUM.

Test Case 1

Input
7

Output
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
1 - 23 + 4 + 5 + 6 + 7 = 0
1 - 23 + 45 + 67 = 0


Test Case 2

Input
8

Output
1 + 2 + 3 + 4 - 5 - 6 - 7 + 8 = 0
1 + 2 + 3 - 4 + 5 - 6 + 7 - 8 = 0
1 + 2 - 3 + 4 + 5 + 6 - 7 - 8 = 0
1 + 2 - 3 - 4 - 5 - 6 + 7 + 8 = 0
1 + 23 - 45 + 6 + 7 + 8 = 0
1 - 2 + 3 - 4 - 5 + 6 - 7 + 8 = 0
1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 = 0
1 - 2 - 3 + 4 - 5 + 6 + 7 - 8 = 0
1 - 23 - 4 + 5 + 6 + 7 + 8 = 0
12 - 34 - 56 + 78 = 0

You may test this program by entering the integer from the keyboard.


Thanks, your help is highly appreciated...
Have you tried to create an algorithm that will calculate the zero-sum?
Mar 11 '07 #2
Have you tried to create an algorithm that will calculate the zero-sum?
I guess not. I did not know that there's an algorithm that does that. I'll just try to research for it here at thescripts or on the internet. but if you have an idea on how to go about that algorithm, i will be delighted if you could teach me...

Thanks for the reply...
Mar 12 '07 #3
sicarie
4,677 Expert Mod 4TB
I guess not. I did not know that there's an algorithm that does that. I'll just try to research for it here at thescripts or on the internet. but if you have an idea on how to go about that algorithm, i will be delighted if you could teach me...

Thanks for the reply...
The easiest way (I don't know of an elegant one off the top of my head) would be to brute force it.

Please correct me if I'm wrong - I might be completely off with this, but it seems to me that you give it the number you want the max to be - between 1 and 9 - and it will then print out every combination of one or two digits that add/subtract to 0 when done in sequence, using all the numbers.

So you would start with (for example, given 3),
Expand|Select|Wrap|Line Numbers
  1. 1+2+3 = 6
  2. 1+2-3 = 0
  3. 1-2+3 = 4
  4. 1-2-3 = -4
  5. 12 + 3 = 15
  6. 12 - 3 = 9
  7. 1 + 23 = 24
  8. 1 - 23 = -22
  9.  
In which case you would only print out 1+2-3. Does that sound like a good summarization of your algorithm?
Mar 12 '07 #4
The easiest way (I don't know of an elegant one off the top of my head) would be to brute force it.

Please correct me if I'm wrong - I might be completely off with this, but it seems to me that you give it the number you want the max to be - between 1 and 9 - and it will then print out every combination of one or two digits that add/subtract to 0 when done in sequence, using all the numbers.

So you would start with (for example, given 3),
Expand|Select|Wrap|Line Numbers
  1. 1+2+3 = 6
  2. 1+2-3 = 0
  3. 1-2+3 = 4
  4. 1-2-3 = -4
  5. 12 + 3 = 15
  6. 12 - 3 = 9
  7. 1 + 23 = 24
  8. 1 - 23 = -22
  9.  
In which case you would only print out 1+2-3. Does that sound like a good summarization of your algorithm?
Yes, you got it correct. My problem now is how to insert those plus and minus on my codes. My knowledge on java would only allow me to test your code(your example) one-by-one. It is easier to do it when N is only equal to 3 or 4, but when N is higher, the combinations also rise exponentially, my code would also be very long. . . I was wondering if there's a shorter way to do it....
Mar 12 '07 #5
sicarie
4,677 Expert Mod 4TB
Yes, you got it correct. My problem now is how to insert those plus and minus on my codes. My knowledge on java would only allow me to test your code(your example) one-by-one. It is easier to do it when N is only equal to 3 or 4, but when N is higher, the combinations also rise exponentially, my code would also be very long. . . I was wondering if there's a shorter way to do it....
Yeah, like I said, I can't think of any real elegant way to do that - I would suggest brute forcing it at first - also looking at any patterns that might arise when you do them - such as how you calculate when there is a plus sign, or when there is a minus sign.

Anyone else have a good idea for a more elegant algorithm than brute force? (I'm sure there is a way to do it, but it'll take me a bit to figure it out...)
Mar 12 '07 #6
Yeah, like I said, I can't think of any real elegant way to do that - I would suggest brute forcing it at first - also looking at any patterns that might arise when you do them - such as how you calculate when there is a plus sign, or when there is a minus sign.

Anyone else have a good idea for a more elegant algorithm than brute force? (I'm sure there is a way to do it, but it'll take me a bit to figure it out...)
hehe... I might do your suggestion, the not so elegant but doable brute forcing.

Thanks to you, goodluck for me...
Mar 12 '07 #7
Ganon11
3,652 Expert 2GB
You could try putting the integers into an array, and the operators into a second array (whose length will be numArray.length - 1). Now, the operator array can be an integer array, with each value being either 0 (addition), 1 (subtraction), or 2(nothing). Then you can iterate through the number array, using the appropriate value from the operator array to evaluate the numbers.

When you finish one execution, you can 'increment' the array to hold the next possibility of operators. You could probably write a method that will give you the next permutation of the operator array.
Mar 12 '07 #8
You could try putting the integers into an array, and the operators into a second array (whose length will be numArray.length - 1). Now, the operator array can be an integer array, with each value being either 0 (addition), 1 (subtraction), or 2(nothing). Then you can iterate through the number array, using the appropriate value from the operator array to evaluate the numbers.

When you finish one execution, you can 'increment' the array to hold the next possibility of operators. You could probably write a method that will give you the next permutation of the operator array.

Yeah, this make a little bit sense to me, thank you guys....
Mar 12 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
15
by: I wish | last post by:
#include <string.h> int a; memset( a, 0, sizeof(a) ); Does that guarantee all bits zero? -- |
25
by: Mantorok Redgormor | last post by:
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. beyond this...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
15
by: Tomás | last post by:
Is the following fully legal and fully portable for all the unsigned types? The aim of the function is to take an array by reference and set each element's value to zero. #include <... ...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.