PROBLEM LINKAuthor: Hanlin Ren DIFFICULTYSIMPLE PREREQUISITIESnone PROBLEMGiven the sequence of numbers on the top face of a die rolled $90^\circ$ at a time, find one possible configuration of numbers on the faces of the die or determine that it doesn't exist. QUICK EXPLANATIONBruteforce all possible answers, check if no step corresponds to rotation to the same face or the opposite face. EXPLANATIONThis problem can be misleading, since it may seem at first that the orientation of the die matters. Actually, it doesn't, since if the number on the top of the die is $x$ and the number on the bottom face is $y$, then it's possible to get any of the remaining 4 numbers can be on top after exactly one step; of course, it's impossible to get $y$ on top after one step and we can't keep $x$ on top either. We don't actually know the pairs of numbers on opposite faces, but there aren't many choices, so we can bruteforce them  pick three unordered pairs and ensure they contain each number 1 through 6 exactly once. There are only $\frac{1}{3!}\frac{6\cdot5}{2}\frac{4\cdot3}{2}\frac{2\cdot1}{2}=15$ such triples of disjoint pairs. Therefore, all we need to do is try all possible configurations of numbers on the die and for each of them, check if the conditions $A_i \neq A_{i+1}$, $A_i \neq o(A_{i+1})$ hold for all $1 \le i < N$. We actually found all possible configurations this way, not just one. There are $O(1)$ configurations, so the time complexity is $O(N)$, same with memory complexity. We can improve this to $O(1)$ memory either by checking which configurations satisfy the constraints as we read the input, or by remembering only how many times each pair $(A_i,A_{i+1})$ appeared. AUTHOR'S AND TESTER'S SOLUTIONSasked 30 Sep '17, 18:55

We know that the next number coming on the top will never be the opposite of the previous number , for eg, if the input is, 1 3 4 ,then opp(1) will not be eq to 3 . opp(3) will not be equal to 4 and 1 . opp(4) will not be equal to 3. So this is what is done in the 2D array g, Here we are denoting 1 to show that the numbers will not be opposite to each other .[ for the above example g[1][3]=1 ,g[3][1]=1.... etc ]. next_permutation gives the next lexographic combination. if there is an array a=[1,2,3,4,5] ,next_permutation(a,a+5) will give me [2,1,3,4,5] and in next iteration will give [2,3,1,4,5 ] and so on. In this array p we will have the final result. So we keep on shuffling untill all elements index pos is not equal to that number itself . the next thing is , if opp[x]=y then opp[y] will be x. So that what we are checking using the condition (p[p[i]]==i) , if this condition is satisfied then i will be greater than 6, now atlast we will check if the array p is legit by crosschecking it with the 2D array 'g' , answered 02 Oct '17, 00:15

I did this question trying all possible position reccursively. It passed the first sub task case but showed wrong ans for the 2nd subtask.Please tell me where I went wrong. Link to my ans is: https://www.codechef.com/viewsolution/15568740 Can anybody please tell me the test case for where it went wrong. Thankx in advance. answered 01 Oct '17, 09:55

your algorithm is too slow to pass the second subtask answered 01 Oct '17, 10:02

In the setter's solution, in the worst case it will enumerate all the permutations whose complexity is 6! = 720. Am i right?
link
This answer is marked "community wiki".
answered 05 Oct '17, 11:15
Yes, but checking each takes O(1) and the total time complexity is O(N) regardless.
(05 Oct '17, 19:21)

Here is the explanation for the setter's code : #include <bits stdc++.h=""> using namespace std;
answered 06 Oct '17, 01:22

The problem can also be solved using backtracking and recursion. Here is a recursive implementation of the same. answered 06 Oct '17, 20:57

The setters should comment their code for readability. answered 28 Jun '18, 13:02

solutions are not opening please resolve the problem
Why practice problem and submitted solutions of this problem are not visible? also other lunchtime problems are not visible
solutions not visible and problem is not available in practice section :/