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.
Sequence Equation
Sequence Equation
+ 0 comments function permutationEquation(p) { // Write your code here var map = new Map(), ar = []; p.forEach((v,i) => map.set(v, i+1)) for(let i=1; i<=p.length; i++) ar.push( map.get ( map.get(i) )); return ar;
+ 0 comments Here is my O(N) c++ solution, you can watch the explanation here : https://youtu.be/D9nfVOmmv7Q
vector<int> permutationEquation(vector<int> p) { vector<int> indexes(p.size() + 1), result; for(int i = 1; i <= p.size(); i++) indexes[p[i-1]] = i; for(int i = 1; i <= p.size(); i++) result.push_back(indexes[indexes[i]]); return result; }
+ 0 comments vector<int> permutationEquation(vector<int> p) { vector<int>ans; for(int i=1;i<=p.size();i++){ int it=find(p.begin(),p.end(),i)-p.begin(); int iit=find(p.begin(),p.end(),it+1)-p.begin(); ans.push_back(iit+1); } return ans; }
+ 0 comments c# version
public static List<int> permutationEquation(List<int> p) { var rs = new List<int>(); for(int x = 1, n = p.Count; x <= n; x++) { int y = p.IndexOf(x) + 1; rs.Add(p.IndexOf(y) + 1); } return rs; }
+ 0 comments pythnon:
def permutationEquation(p): result = [0] * len(p) for i in range(len(p)): result[i] = p.index(p.index(i+1) + 1) + 1 return result
Load more conversations
Sort 1314 Discussions, By:
Please Login in order to post a comment