VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-number-of-accepted-invitations-to-party/

⇱ Maximum Number of Accepted Invitations to Party - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum Number of Accepted Invitations to Party

Last Updated : 23 Jul, 2025

Given a grid of m boys and n girls, where grid[i][j] = 1 represents that ith boy can invite girl jth girl to party. Each boy can invite only one girl and each girl can accept only one invitation. Find the most invitations that can be accepted.

Example:

Input: grid = {{1,1,1},
{1,0,1},
{0,0,1}}
Output: 3
Explanation: The invitations can be sent as follows:
=> 1st boy invites the 2nd girl.
=> 2nd boy invites the 1st girl.
=> 3rd boy invites the 3rd girl.

Input: grid = {{1,0,1,0},
{1,0,0,0},
{0,0,1,0},
{1,1,1,0}}
Output: 3
Explanation: The invitations can be sent as follows:
=> 1st boy invites the 3rd girl.
=> 2nd boy invites the 1st girl.
=> 3rd boy invites no one.
=> 4th boy invites the 2nd girl.

Approach:

The solution uses the concept of maximum bipartite matching. We have to iterating over each boy and trying to find a girl that he can invite. If a boy finds a girl who hasn’t been invited yet, he invites her. If the girl has already been invited by another boy, then we recursively check if there is another girl that the other boy can invite. If yes, then the current boy invites the girl, and the other boy invites the other girl. This process is repeated until all boys have been checked.

The key idea here is to use depth-first search (DFS), we can efficiently backtrack and find alternative matches if a certain path doesn’t lead to a solution. This will makes sure that we find the maximum possible number of matches.

Steps-by-step approach:

  • Create a function canInviteGirl(), which recursively checks if the current boy can invite any girl:
    • It uses backtracking to explore possible combinations.
    • Check if a girl is successfully invited then returns true
    • otherwise, returns false.
  • Create a function maximumInvitations(), which iterates through each boy.
    • For each boy, calls canInviteGirl() to check if the boy can invite a girl.
    • Maintains a count of successful invitations and returns the total count.

Below are the implementation of the above approach:


Output
The maximum possible number of accepted invitations is: 3

Time complexity: O(m * n), where m is the number of boys and n is the number of girls.
Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: