C++ | Backtracking | Beginner friendly
Since constraints were very small\n\nm == students.length == mentors.length\nn == students[i].length == mentors[j].length\n1 <= m, n <= 8\nstudents[i][k] is eit
Since constraints were very small\n```\nm == students.length == mentors.length\nn == students[i].length == mentors[j].length\n1 <= m, n <= 8\nstudents[i][k] is either 0 or 1.\nmentors[j][k] is either 0 or 1.\n```\n\nthat\'s why I decided to go with the backtracking approach.\n\n```\nclass Solution {\npublic:\n\t// to calculate score of the student-mentor pair\n    int score(vector<int>&s, vector<int>&m){\n        int cnt=0;\n        for(int i=0;i<s.size();i++){\n            if(s[i]==m[i]) cnt++;\n        }\n        return cnt;\n    }\n    \n    int solve(vector<vector<int>>& s, vector<vector<int>>& m, int student, int sz, vector<bool>&vis){\n \xA0 \xA0 \xA0 \xA0if(student==sz) return 0; // if all pairs are done\n        int ans=0;\n        for(int i=0;i<sz;i++){\n            if(!vis[i]){\n                vis[i]=1;\n                ans=max(ans,score(s[student],m[i])+solve(s,m,student+1,sz,vis)); // pass next student(student+1)\n                vis[i]=0;\n            }\n        }\n        return ans;\n    }\n    \n    int maxCompatibilitySum(vector<vector<int>>& s, vector<vector<int>>& m) {\n        int ans=0;\n        vector<bool>vis(m.size(),0); // to keep track of already selected mentors\n        for(int i=0;i<m.size();i++){\n            vis[i]=1; // select ith mentor\n            ans=max(ans,score(s[0],m[i])+solve(s,m,1,s.size(),vis)); // calculate score of student and selected mentor and then solve for other pairs\n            vis[i]=0; // deselect(backtrack)\n        }\n        return ans;\n    }\n};\n```
[]