Program to find minimum number of people to teach in Python

Suppose we have a number n, an array called languages, and an array called friendships. There are n languages numbered from 1 through n, languages[i] represents a set of languages the ith user knows, and friendships[i] holds a pair [ui, vi] denoting a friendship between users ui and vi. We can select one language and teach it to some users so that all friends can communicate with each other. We have to find the minimum number of users required to teach.

Note that friendships are not transitive, so if x is a friend of y and y is a friend of z, this doesn't mean that x is a friend of z.

Example

If the input is like n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]], then the output will be 2 because we need to teach language 3 to users 1 and 3.

Algorithm Steps

To solve this problem, we will follow these steps ?

  • Create a list of sets representing languages known by each user
  • Find all users who cannot communicate with their friends (have no common languages)
  • If no such users exist, return 0
  • Count the frequency of each language among users who need to learn
  • Choose the most frequent language and calculate minimum users to teach

Implementation

from collections import Counter

def solve(n, languages, friendships):
    # Convert languages to sets for easier operations
    lang = [set(L) for L in languages]
    
    # Find users who cannot communicate with friends
    not_comm = set()
    for a, b in friendships:
        a -= 1  # Convert to 0-based indexing
        b -= 1
        if lang[a].isdisjoint(lang[b]):  # No common languages
            not_comm.add(a)
            not_comm.add(b)
    
    # If everyone can communicate, no teaching needed
    if not not_comm:
        return 0
    
    # Count frequency of each language among users who need to learn
    cnt = Counter()
    for person in not_comm:
        cnt.update(lang[person])
    
    # Find the most frequent language
    temp = max(cnt.values())
    
    # Return minimum users to teach (total - those who already know the chosen language)
    return len(not_comm) - temp

# Test the solution
n = 3
languages = [[2], [1,3], [1,2], [3]]
friendships = [[1,4], [1,2], [3,4], [2,3]]

result = solve(n, languages, friendships)
print(f"Minimum number of people to teach: {result}")
Minimum number of people to teach: 2

How It Works

The algorithm works by first identifying friend pairs who cannot communicate (have no common languages). Then it counts how many of these users already know each language. The optimal strategy is to teach the language that the maximum number of these users already know, minimizing the number of new learners needed.

In our example, users 1 and 3 need to learn a language. Language 3 is known by user 3, so we only need to teach it to users 1 and 3 (2 people total).

Conclusion

This greedy approach finds the minimum number of people to teach by selecting the language known by the most users who need communication. The time complexity is O(F + U×L) where F is friendships, U is users, and L is average languages per user.

Updated on: 2026-03-26T14:31:53+05:30

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements