VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimize-number-of-notes-required-to-be-distributed-among-students/

⇱ Minimize number of notes required to be distributed among students - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimize number of notes required to be distributed among students

Last Updated : 23 Jul, 2025

Given an array arr[] consisting of N strings representing the name of the students in the class and another array of pairs P[][2] such that P[i][0] likes P[i][1], the task is to find the minimum number of notes to be distributed in the class such that sharing of notes can take place only if a student likes another student either directly or indirectly.

Examples:

Input: arr[] = {geeks, for, code, run, compile}, P[][] = {{geeks, for}, {for, code}, {code, run}, {run, compile}, {run, for}}
Output: 3
Explanation:
Below is the image to represent the relationship among the students:

👁 Image

From the above image:

  1. Students named {"for", "code", "run"} require a single copy of notes, since there exists a mutual relationship between them.
  2. Student named {"geeks"} requires a single copy of notes.
  3. Student named {"compile"} also require a single copy of notes.

So, the minimum number of notes required is 3.

Input: arr[] = {geeks, for, all, run, debug, compile}, P[][] = {{geeks, for}, {for, all}, {all, geeks}, {for, run}, {run, compile}, {compile, debug}, {debug, run}}
Output: 2

Approach: The given problem can be solved by finding the number of strongly connected components in a directed graph after generating the relationship graph with the given conditions. Follow the steps below to solve the problem:

Below is the implementation of the above approach:


Output: 
3

 

Time Complexity: O(N + M)
Auxiliary Space: O(N) 


 

Comment