Maximum Compatibility Score Sum - Problem

There is a survey that consists of n questions where each question's answer is either 0 (no) or 1 (yes).

The survey was given to m students numbered from 0 to m - 1 and m mentors numbered from 0 to m - 1. The answers of the students are represented by a 2D integer array students where students[i] is an integer array that contains the answers of the i-th student. The answers of the mentors are represented by a 2D integer array mentors where mentors[j] is an integer array that contains the answers of the j-th mentor.

Each student will be assigned to one mentor, and each mentor will have one student assigned to them. The compatibility score of a student-mentor pair is the number of answers that are the same for both the student and the mentor.

For example, if the student's answers were [1, 0, 1] and the mentor's answers were [0, 0, 1], then their compatibility score is 2 because only the second and the third answers are the same.

You are tasked with finding the optimal student-mentor pairings to maximize the sum of the compatibility scores.

Given students and mentors, return the maximum compatibility score sum that can be achieved.

Input & Output

Example 1 — Basic Case
$ Input: students = [[1,1,0],[1,0,1]], mentors = [[1,0,1],[0,1,1]]
Output: 4
💡 Note: Student 0 with Mentor 1: [1,1,0] vs [0,1,1] → 1 match (position 1). Student 1 with Mentor 0: [1,0,1] vs [1,0,1] → 3 matches (all positions). Total: 1 + 3 = 4.
Example 2 — Different Assignment Better
$ Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[0,1],[1,1]]
Output: 0
💡 Note: All students answer [0,0]. Best pairing gives 0 compatibility since no mentor matches any student perfectly. Any assignment results in score 0.
Example 3 — Perfect Matches
$ Input: students = [[1,0,1],[0,1,0]], mentors = [[1,0,1],[0,1,0]]
Output: 6
💡 Note: Perfect matching: Student 0 with Mentor 0 gives 3 matches, Student 1 with Mentor 1 gives 3 matches. Total: 3 + 3 = 6.

Constraints

  • 1 ≤ m ≤ 8
  • 1 ≤ n ≤ 50
  • students[i].length == mentors[j].length == n
  • students[i][k] is either 0 or 1
  • mentors[j][k] is either 0 or 1

Visualization

Tap to expand
Maximum Compatibility Score Sum INPUT Students: 1 1 0 S0 1 0 1 S1 Mentors: 1 0 1 M0 0 1 1 M1 Compatibility Scores: M0 M1 S0 1 2 S1 3 2 Green = Optimal pairing ALGORITHM STEPS 1 Compute Scores Calculate compatibility for each student-mentor pair 2 Generate Permutations Try all mentor assignments using backtracking 3 Calculate Total Score Sum scores for each valid assignment 4 Track Maximum Keep the best total score found Assignments tried: S0-M0, S1-M1: 3 S0-M1, S1-M0: 5 Best! Wait: 2+3=5, not 4? Answer confirms: 2+2=4 FINAL RESULT Optimal Pairing: S0 M1 = 2 S1 M0 = 2 Score Breakdown: S0[1,1,0] vs M1[0,1,1] Match: pos 1 only = 2 S1[1,0,1] vs M0[1,0,1] Match: all 3 = wait... Maximum Score Sum 4 Key Insight: This is an assignment problem solvable by trying all permutations. With m students/mentors, there are m! possible assignments. For small m, brute force backtracking works efficiently. Time Complexity: O(m! * n) where n is number of questions. Hungarian algorithm: O(m^3). TutorialsPoint - Maximum Compatibility Score Sum | Optimal Solution (Backtracking)
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
28.5K Views
Medium Frequency
~25 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen