<\/span><\/h2>\n\n\n\nIn this challenge, we practice calculating Spearman’s rank correlation coefficient<\/em>.<\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nGiven two n<\/em><\/strong>-element data sets, X<\/em><\/strong> and Y<\/em><\/strong>, calculate the value of Spearman’s rank correlation coefficient.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer, n<\/em><\/strong>, denoting the number of values in data sets X<\/em><\/strong> and Y<\/em><\/strong>.
The second line contains n<\/em><\/strong> space-separated real numbers (scaled to at most<\/em> one decimal place) denoting data set X<\/em><\/strong>.
The third line contains n<\/em><\/strong> space-separated real numbers (scaled to at most<\/em> one decimal place) denoting data set Y<\/em><\/strong>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 10 <= n<\/em> <= 100<\/strong><\/li>
- 1 <= xi<\/sub><\/em> <= 500<\/strong>, where xi<\/sub><\/em><\/strong> is the ith<\/sup><\/strong><\/em> value of data set X<\/em><\/strong>.<\/li>
- 1 <= yi<\/sub><\/em> <= 500<\/strong>, where yi<\/sub><\/em><\/strong> is the ith<\/sup><\/strong><\/em> value of data set Y<\/em><\/strong>.<\/li>
- Data set X<\/em><\/strong> contains unique values.<\/li>
- Data set Y<\/em><\/strong> contains unique values.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the value of the Spearman’s rank correlation coefficient, rounded to a scale of 3<\/strong> decimal places.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n10\n10 9.8 8 7.8 7.7 1.7 6 5 1.4 2 \n200 44 32 24 22 17 15 12 8 4<\/code><\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\n0.903<\/code><\/pre>\n\n\n\n<\/span>Solution – Spearman’s Rank Correlation Coefficient<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\n#include <map>\nusing namespace std;\n\n\n\/*\n rank = 1 - (6*summation(x[i]-y[i])^2)\/n*(n^2-1);\n*\/\n\nvector<double> build_rank(double x[], int n)\n{\n map<double, int> m1;\n vector<double> x1(n);\n \n for(auto i=0; i<n; i++) \n {\n m1.insert(make_pair(x[i], i));\n }\n \n int rank = 0;\n for(auto it : m1)\n {\n x1[it.second] = rank++;\n }\n \n return x1;\n}\ndouble supermans_rank(double x[], double y[], int n)\n{ \n double num = 0;\n \n auto rank_x = build_rank(x, n);\n auto rank_y = build_rank(y, n);\n \n for(auto i=0; i<n; i++)\n {\n num += ((rank_x[i]-rank_y[i])*(rank_x[i]-rank_y[i]));\n }\n num *= 6.0;\n \n double den = (n*(n*n - 1));\n \n double res = num\/den;\n \n res = 1 - res;\n \n return res; \n}\n\nint main() {\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n \n int n; cin>>n; double x[n+4], y[n+4];\n \n for(auto i=0; i<n; i++)\n {\n cin>>x[i];\n \/\/sum1 += x[i];\n }\n \n for(auto i=0; i<n; i++)\n {\n cin>>y[i];\n \/\/sum3 += y[i];\n }\n \n double supermans_rank_coeff = supermans_rank(x, y, n);\n \n printf(\"%0.3f\", supermans_rank_coeff);\n\n \n return 0;\n}\n<\/pre>\n\n\n\n