How about this ? 1. We construct a graph with these nodes. 2. We make an incoming edge on a node if it is created using some other node. Ex: 7=5+2. So 7 will have 1 incoming and 5 & 2 will have outgoing. 3. Select all the nodes which only have outgoing edges. These would be the actual distances between the nodes. 4. If the total number of nodes selected in step 3 are equal to milestones, then goto step 6. 5. Remove all the nodes from the graph which have only outgoing edges and goto step 3. 6. Rearrange the nodes in the correct order as per the distances given.
On Jul 7, 9:23 am, Akshata Sharma <akshatasharm...@gmail.com> wrote: > There is a straight roads with 'n' number of milestones. You are given an > array with the distance between all the pairs of milestones in some random > order. Find the position of milestones. > Example: > Consider a road with 4 milestones(a,b,c,d) : > a <--- 3Km --->b<--- 5Km --->c<--- 2Km --->d > Distance between a and b = 3 > Distance between a and c = 8 > Distance between a and d = 10 > Distance between b and c = 5 > Distance between b and d = 7 > Distance between c and d = 2 > All the above values are given in a random order say 7, 10, 5, 2, 8, 3. > The output must be 3,5,2 or 2,5,3 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.