<\/span><\/h2>\n\n\n\nGiven a sequence of n<\/strong><\/em> integers, p<\/em>(1), p<\/em>(2), . . . , p<\/em>(n<\/em>)<\/strong> where each element is distinct and satisfies 1 <= p<\/em>(x<\/em>) <= n<\/em><\/strong>. For each<\/em> x<\/strong><\/em> where 1 <= x<\/em> <= n<\/em><\/strong>, that is x<\/strong><\/em> increments from 1<\/strong> to n<\/strong><\/em>, find any integer y<\/strong><\/em> such that p<\/em>(p<\/em>(y<\/em>)) = x<\/em><\/strong> and keep a history of the values of y<\/strong> in a return array.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\np<\/em> = [5, 2, 1, 3, 4]<\/strong><\/p>\n\n\n\nEach value of x<\/strong><\/em> between 1<\/strong> and 5<\/strong>, the length of the sequence, is analyzed as follows:<\/p>\n\n\n\n- x<\/em> = 1 = p<\/em>[3],p<\/em>[4] = 3<\/strong>, so p<\/em>[p<\/em>[4]] = 1<\/strong><\/li>
- x<\/em> = 2 = p<\/em>[2],p<\/em>[2] = 2<\/strong>, so p<\/em>[p<\/em>[2]] = 2<\/strong><\/li>
- x<\/em> = 3 = p<\/em>[4],p<\/em>[5] = 4<\/strong>, so p<\/em>[p<\/em>[5]] = 3<\/strong><\/li>
- x<\/em> = 4 = p<\/em>[5],p<\/em>[1] = 5<\/strong>, so p<\/em>[p<\/em>[1]] = 4<\/strong><\/li>
- x<\/em> = 5 = p<\/em>[1],p<\/em>[3] = 1<\/strong>, so p<\/em>[p<\/em>[3]] = 5<\/strong><\/li><\/ol>\n\n\n\n
The values for y<\/strong><\/em> are [4, 2, 5, 1, 3]<\/strong>.<\/p>\n\n\n\nFunction Description<\/strong><\/p>\n\n\n\nComplete the permutationEquation<\/em> function in the editor below.<\/p>\n\n\n\npermutationEquation has the following parameter(s):<\/p>\n\n\n\n
- int p[n]:<\/em> an array of integers<\/li><\/ul>\n\n\n\n
Returns<\/strong><\/p>\n\n\n\n- int[n]:<\/em> the values of y<\/strong><\/em> for all x<\/strong><\/em> in the arithmetic sequence 1<\/strong> to n<\/em><\/strong><\/li><\/ul>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer n<\/strong><\/em>, the number of elements in the sequence.
The second line contains n<\/strong><\/em> space-separated integers p<\/em>[i<\/em>]<\/strong> where 1 <= i<\/em> <= n<\/em><\/strong>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= n<\/em> <= 50<\/strong><\/li>
- 1 <= p<\/em>[i<\/em>] <= 50<\/strong>, where 1 <= i<\/em> <= n<\/em><\/strong>.<\/li>
- Each element in the sequence is distinct.<\/li><\/ul>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\n3\n2 3 1<\/code><\/pre>\n\n\n\nSample Output 0<\/strong><\/p>\n\n\n\n2\n3\n1<\/code><\/pre>\n\n\n\nExplanation 0<\/strong><\/p>\n\n\n\nGiven the values of p<\/em>(1) = 2<\/strong>, p<\/em>(2) = 3<\/strong>, and p<\/em>(3) = 1<\/strong>, we calculate and print the following values for each x<\/strong><\/em> from 1 to n<\/em><\/strong>:<\/p>\n\n\n\n- x<\/strong><\/em> = 1 = p<\/em>(3) = p<\/em>(p<\/em>(2)) = p<\/em>(p<\/em>(y))<\/strong>, so we print the value of y<\/em> = 2<\/strong> on a new line.<\/li>
- x<\/strong><\/em> = 2 = p<\/em>(1) = p<\/em>(p<\/em>(3)) = p<\/em>(p<\/em>(y))<\/strong>, so we print the value of y<\/em> = 3<\/strong> on a new line.<\/li>
- x<\/strong><\/em> = 3 = p<\/em>(2) = p<\/em>(p<\/em>(1)) = p<\/em>(p<\/em>(y))<\/strong>, so we print the value of y<\/em> =1<\/strong> on a new line.<\/li><\/ol>\n\n\n\n
Sample Input 1<\/strong><\/p>\n\n\n\n5\n4 3 5 1 2<\/code><\/pre>\n\n\n\nSample Output 1<\/strong><\/p>\n\n\n\n1\n3\n5\n4\n2<\/code><\/pre>\n\n\n\n<\/span>Solution – Sequence Equation – HackerRank Solution<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <bits\/stdc++.h>\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair<int, int> II;\n\nint main() {\n #ifdef LOCAL\n freopen(\"Data.inp\", \"r\", stdin);\n freopen(\"Data.out\", \"w\", stdout);\n #endif\n\n int n, a[100];\n cin >> n;\n for (int i = 1; i <= n; ++i) cin >> a[i];\n\n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) if (a[a[j]] == i) {\n cout << j << endl;\n break;\n }\n }\n\n return 0;\n}\n<\/pre>\n\n\n\n<\/span>Python<\/strong><\/span><\/h3>\n\n\n\n#!\/usr\/bin\/env python3\n\nimport sys\n\ndef permutationEquation(p):\n output = []\n \n for num in range(1, max(p)+1):\n output.append(p.index(p.index(num)+1)+1)\n \n return output\n \n\nif __name__ == \"__main__\":\n n = int(input().strip())\n p = list(map(int, input().strip().split(' ')))\n result = permutationEquation(p)\n print (\"\\n\".join(map(str, result)))\n\n<\/pre>\n\n\n\n<\/span>Java<\/strong><\/span><\/h3>\n\n\n\nimport java.io.*;\nimport java.util.*;\nimport java.text.*;\nimport java.math.*;\nimport java.util.regex.*;\n\npublic class Solution {\n public static void main(String args[] ) throws Exception {\n Scanner sc = new Scanner(System.in);\n int n = sc.nextInt();\n int arr[]=new int[n];\n for(int i = 0 ;i<n;i++)\n arr[i]=sc.nextInt();\n for(int i = 0 ;i<n;i++)\n {\n int pos = 0;\n for(int j = 0 ; j<n;j++)\n {\n if(arr[j]==i+1)\n {\n pos = j+1 ;\n break;\n }\n }\n int pos1=0;\n for(int j = 0 ; j<n;j++)\n {\n if(arr[j]==pos)\n {\n pos1 = j ;break;\n }\n }\n System.out.println(pos1+1);\n }\n }\n}\n<\/pre>\n\n\n\nNote:<\/strong> This problem (