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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: can someone solve this problem

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default 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. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: can someone solve this problem

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

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    shilpy (September 3rd, 2014)

  9. #7
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: can someone solve this problem

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

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    shilpy (September 3rd, 2014)

  12. #9
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: can someone solve this problem

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

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    shilpy (September 3rd, 2014)

  15. #11
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default 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. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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:
    -+
    --
    +-
    If you don't understand my answer, don't ignore it, ask a question.

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

    shilpy (September 4th, 2014)

  18. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #14
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default 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...

Similar Threads

  1. Please solve my problem
    By Supun Pramoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 04:04 PM
  2. please solve the problem
    By liz in forum Threads
    Replies: 1
    Last Post: February 6th, 2014, 05:27 AM
  3. Please solve my problem
    By Supun Pramoda in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 8th, 2013, 12:14 AM
  4. please solve my problem
    By anarica in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2013, 07:35 AM
  5. Need help to solve this problem
    By ahanf in forum Object Oriented Programming
    Replies: 1
    Last Post: June 3rd, 2012, 05:00 AM