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 0: [1,1,0] vs [1,0,1] → 2 matches (positions 0). Student 1 with Mentor 1: [1,0,1] vs [0,1,1] → 2 matches (positions 2). Total: 2 + 2 = 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 SumStudentsS0: [1, 1, 0]S1: [1, 0, 1]MentorsM0: [1, 0, 1]M1: [0, 1, 1]Compatibility MatrixS0→M0: 2 matches, S0→M1: 1 matchS1→M0: 2 matches, S1→M1: 2 matchesBest: S0→M0 + S1→M1 = 4Optimal Assignment Process1. Calculate all pairwise compatibility scores2. Use DP with bitmask to find best assignmentKey InsightThis is a bipartite matching problemSmall m allows exponential DP solutionOutput: Maximum possible compatibility sum = 4
Understanding the Visualization
1
Input Surveys
Students and mentors with binary answers to questions
2
Calculate Compatibility
Count matching answers between each student-mentor pair
3
Optimal Assignment
Find pairing that maximizes total compatibility score
Key Takeaway
🎯 Key Insight: This assignment problem can be solved efficiently using dynamic programming with bitmasks since m ≤ 8
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