We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
p(x) is a one-to-one function, so it has an inverse. We create the function representing the inverse of p(x), and represent it as an array: int [] p_inverse
We need to find a y where p(p(y)) = x. This equation can be rewritten as y = p_inverse(p_inverse(x)), which is the version of the equation we use to calculate and print y.
importjava.util.Scanner;publicclassSolution{publicstaticvoidmain(String[]args){/* Create function: p_inverse */Scannerscan=newScanner(System.in);intn=scan.nextInt();int[]p_inverse=newint[n+1];for(intx=1;x<=n;x++){intpx=scan.nextInt();p_inverse[px]=x;}scan.close();/* Calculate and print each y */for(intx=1;x<=n;x++){inty=p_inverse[p_inverse[x]];System.out.println(y);}}}
Sequence Equation
You are viewing a single comment's thread. Return to all comments →
Simple solution with explanation
From my HackerRank solutions.
Our input provides us n values of x and p(x)
p(x) is a one-to-one function, so it has an inverse. We create the function representing the inverse of p(x), and represent it as an array: int [] p_inverse
We need to find a y where p(p(y)) = x. This equation can be rewritten as
y = p_inverse(p_inverse(x)), which is the version of the equation we use to calculate and print y.
Let me know if you have any questions.