Thread: Necklace (combinatorics)

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    5

    Necklace (combinatorics)

    Necklace maker has currently b -blue rubies, g -green rubies, r-red rubies and y -yellow rubies. He has to arrange the rubies next to each other in a straight line to make the necklace. But, there are a couple of rules to be followed while making this necklace:

    A blue ruby should be followed by either a blue ruby or a red ruby
    A green ruby should be followed by either a green ruby or a yellow ruby
    A red ruby should be followed by either a green ruby or a yellow ruby
    A yellow ruby should be followed by either a blue ruby or a red ruby


    Can you tell what is the maximum possible length of the necklace that Necklace maker can make. The length of a necklace is the number of rubies in it.

    Input Format
    The first line contains an integer representing b.
    The second line contains an integer representing g.
    The third line contains an integer representing r.
    The fourth line contains an integer representing y.

    Constraints
    0 <= b, g, r, y <= 3000
    At least one of b, g, r, y is greater than 0

    Output Format
    A single integer which is the answer to the problem.
    Last edited by Avinash2408; 03-21-2018 at 03:54 AM. Reason: grammar

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the first part of this is a maths problem, not a programming problem.

    Do you understand enough maths to solve the simple cases on paper?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    No i thought its a Data structures related problem

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What data structures have you considered that might help?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    FWIW, combinatorics usually means a clever algorithm, not a brute force search of some impossibly large data structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    I was thinking about graphs

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    One straightforward way would be to solve it recursively, and of course, memoise the sub-problems or solve it bottom-up dynamically.
    Last edited by uptilldawn; 03-22-2018 at 12:10 AM.

  8. #8
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Will Graph coloring help?

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well the first part of this is a maths problem, not a programming problem.

    Do you understand enough maths to solve the simple cases on paper?
    Any help would be appreciated. Give me some ideas pls

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Avinash2408 View Post
    A blue ruby should be followed by either a blue ruby or a red ruby
    A green ruby should be followed by either a green ruby or a yellow ruby
    A red ruby should be followed by either a green ruby or a yellow ruby
    A yellow ruby should be followed by either a blue ruby or a red ruby
    I would start by writing the rules in the form of
    A yellow ruby can be proceeded by ...

    And, if you can use recursive functions; I would think about how to solve it that way.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would also solve the problem for 1 each of each color and see what that gives you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    One recursive solution, that basically considers all possible arrangements, would look something like this:

    Your parameters should include the last color used and the number of rubies left for each color.

    Based on the last ruby's color, you decide which color is next, call the function recursively by updated parameters, and return a maximum among options.
    Last edited by uptilldawn; 03-23-2018 at 09:43 AM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you start with pencil and paper and say 2 of each colour and practice making longest chains.
    From your practice, you start to develope ideas of how you solve the problem with an algorithm.

    Only then can you think about code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    Transition graph:
       _
      | |
      '-B-->R
        ^  /|
        | / |
        |/  +
        Y<--G-,
            |_|
    
    
      Number of        Longest
      B   R   G   Y    necklace
     10   0   0   0    10
      0  10   0   0     1
      0   0  10   0    10
      0   0   0  10     1
     10  10  10  10    40
      5  10  10  10     ?
     10   5  10  10   ...   interesting
     ...
    A little inaccuracy saves tons of explanation. - H.H. Munro

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I take it the plus sign "+" is a down arrow.

    Tim S.


    Quote Originally Posted by john.c View Post
    Code:
    Transition graph:
       _
      | |
      '-B-->R
        ^  /|
        | / |
        |/  +
        Y<--G-,
            |_|
    
    
      Number of        Longest
      B   R   G   Y    necklace
     10   0   0   0    10
      0  10   0   0     1
      0   0  10   0    10
      0   0   0  10     1
     10  10  10  10    40
      5  10  10  10     ?
     10   5  10  10   ...   interesting
     ...
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic combinatorics
    By Tobias Mihel in forum C++ Programming
    Replies: 3
    Last Post: 05-23-2013, 10:02 AM
  2. Necklaces and Bracelets in Combinatorics
    By Stamatis in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2013, 03:29 PM
  3. Broken Necklace
    By sybariticak47 in forum Windows Programming
    Replies: 6
    Last Post: 01-22-2006, 03:31 PM