find the exact positions of n numbers, if each time only 3 positions individually known are in order

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


find the exact positions of n numbers, if each time only 3 positions individually known are in order



Say if there an array of length N.
We need to find the exact positions of M unique numbers in N positions.



example:
The array of length 19 contains 8 unique numbers was in order like


3|6|1|8|1|2|9|8|6|3|5|1|8|7|1|9|8|7|1



The ARRAY given in top we don't know.
We have set with 3 numbers each like below


3|6|1 2|9|8 6|1|8 9|8|7 1|8|7 1|8|7 6|3|5 8|7|1 3|5|1 8|6|3 1|8|1 5|1|8 9|8|6 8|7|1 7|1|9 1|2|9 1|9|8 8|1|2 1|3|6 7|1|3



Can we backtrack to form the above array with exact positions by using the above set of 3 numbers each at a time.



I tried multiple ways to find the logic but unluckily didn't achieve the exact array given :(



Need the logic in pseudo code or program in any language



NOTE:- This is a circular thing--- like a circular queue created with array





Although not strictly required, it's good practice if you can link to the problem source (if any).
– user202729
37 mins ago





If you have tried multiple ways, try posting some of that code, explaining the problems with it, and people might be able to help you with it. Otherwise, you're asking people to start from scratch.
– khelwood
36 mins ago







See: When is it appropriate to tag multiple languages in my question?
– Patrick Parker
34 mins ago





sorry @user202729 and Khelwood as i mentioned i failed in building logic it self... :( And for Patrick Parker i taged multiple languages because i mentioned pseudo code or programming .... :P
– Praneeth Reddy
21 mins ago





There is an algorithm tag for that.
– user202729
20 mins ago




1 Answer
1



Here is a solution in python. Insert each element of input in a new list res based on if the elements first 2 numbers match any of existing elements last 2 numbers


res


>>> def solve(ll):
... res=
... for e in ll:
... for i,a in enumerate(res):
... if e[:2] == a[1:]:
... res.insert(i+1, e)
... break
... else:
... res.append(e)
... return [e[0] for e in res]
...
>>>
>>> data = '3|6|1 2|9|8 6|1|8 9|8|7 1|8|7 1|8|7 6|3|5 8|7|1 3|5|1 8|6|3 1|8|1 5|1|8 9|8|6 8|7|1 7|1|9 1|2|9 1|9|8 8|1|2 7|1|3'
>>> ll = [list(map(int, sl.split('|'))) for sl in data.split()]
>>> ll
[[3, 6, 1], [2, 9, 8], [6, 1, 8], [9, 8, 7], [1, 8, 7], [1, 8, 7], [6, 3, 5], [8, 7, 1], [3, 5, 1], [8, 6, 3], [1, 8, 1], [5, 1, 8], [9, 8, 6], [8, 7, 1], [7, 1, 9], [1, 2, 9], [1, 9, 8], [8, 1, 2], [7, 1, 3]]
>>> solve(ll)
[3, 6, 1, 8, 1, 8, 7, 7, 1, 8, 1, 2, 9, 9, 6, 3, 5, 8, 1]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

6CF5Znws0GjEJI 5RCU
uvfiFEJNrEjwAUhVWb43,1J 2iAPixTp,4GanLMRGh0wO8,sqfEm,shBEhqBssl jPTMyqi5n IoN36 Um

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham