Find the total guests that are present at the party - GeeksforGeeks

01 Apr, 2021
A person hosts a party and invites N guests to it. However, each guest has a condition, that each guest ‘Gi’ only stays at the party if there are at least ‘Pi’ people already at the party, otherwise leaves. The total number of guests N and the number of people each guest needs ‘Pi’ are given as input for each guest. The task is to find the total guests that are present at the party. It is also given that the guests arrive at the party in the order given in the array ‘Pi’Examples:  
Input: N = 5, Pi = {1, 0, 2, 1, 3}
Output: 2
Explanation: 
Since 5 guests are invited to the party.
Total guest present initially = 0

For Guest number 1:
The 1st guest needs at least 1 person, 
since he is the first to arrive, 
and there is no one else, so he leaves.
Therefore, Total guest so far = 0

For Guest number 2:
The 2nd guest needs 0 people, so he stays. 
Therefore, Total guest so far = 0 + 1 = 1

For Guest number 3:
The 3rd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1

For Guest number 4:
The 4th guest needs at least 1 people,
And there is 1 guest present, so he stays. 
Therefore, Total guest so far = 1 + 1 = 2

For Guest number 5:
The 5th guest needs at least 3 people,
And there is only 2 guest present, so he leaves. 
Therefore, Total guest so far = 2 + 0 = 2

Total guests that are present at the party = 2.

Input: N = 3, Pi = {0, 2, 1}
Output: 2
Explanation: 
Since 3 guests are invited to the party.
Total guest present initially = 0

For Guest number 1:
The 1st guest needs 0 people, so he stays.
Therefore, Total guest so far = 1

For Guest number 2:
The 2nd guest needs at least 2 people,
And there are still only 1 guest present,
so he leaves.
Therefore, Total guest so far = 1 + 0 = 1

For Guest number 3:
The 3rd guest needs at least 1 people,
And there is 1 guest present, so he stays. 
Therefore, Total guest so far = 1 + 1 = 2

Total guests that are present at the party = 2.
 
Approach:  
Get the number of guests invited in N and the requirement of each guest in array guest[].
Initialize totalGuests to 0, as the total number of guests present.
Iterate in the array guest[] from 0 to N-1.
If the requirement of the guest is less than or equal to totalGuests, Increment totalGuests by 1
When complete array guest[] has been traversed, print the total number of guests ‘totalGuests’
Implementation:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to get the// total number of guests at the party #include <bits/stdc++.h>using namespace std; // Function to find the totalGuestsint findGuest(int array[], int N){    // Total guest before the party are 0    int count = 0;     // Checking requirements for each guest    for (int i = 0; i < N; i++) {         // If requirements are met        if (array[i] <= count) {             // The Gi guest decides to stay            // So increment total guest by 1            count++;        }    }     // Return the totalnumber of guest    return count;} // Driver codeint main(){     // Get the number of guests invited    int N = 5;     // Guests array stores    // the requirement by each guest    int guests[] = { 1, 0, 2, 1, 3 };     // Get the total number of guests present    int totalGuests = findGuest(guests, N);     cout << totalGuests << endl;     return 0;}
// Java program to get the// total number of guests at the partyclass GFG{     // Function to find the totalGuestsstatic int findGuest(int array[], int N){    // Total guest before the party are 0    int count = 0;     // Checking requirements for each guest    for (int i = 0; i < N; i++)    {         // If requirements are met        if (array[i] <= count)        {             // The Gi guest decides to stay            // So increment total guest by 1            count++;        }    }     // Return the totalnumber of guest    return count;} // Driver codepublic static void main(String[] args){     // Get the number of guests invited    int N = 5;     // Guests array stores    // the requirement by each guest    int guests[] = { 1, 0, 2, 1, 3 };     // Get the total number of guests present    int totalGuests = findGuest(guests, N);     System.out.println(totalGuests);}} // This code is contributed by Code_Mech
# Python3 program to get the# total number of guests at the party # Function to find the totalGuestsdef findGuest(guests, N):    count = 0     # Checking requirements for each guest    for i in range(N):         # If requirements are met        if guests[i] <= count:             # The Gi guest decides to stay            # So increment total guest by 1            count += 1                 # Return the totalnumber of gues    return count # Driver codeN = 5guests = [1, 0, 2, 1, 3]totalGusets = findGuest(guests, N)print(totalGusets) # This code is contributed by Shrikant13
// C# program to get the// total number of guests at the partyusing System; class GFG{             // Function to find the totalGuests    static int findGuest(int [] array, int N)    {        // Total guest before the party are 0        int count = 0;             // Checking requirements for each guest        for (int i = 0; i < N; i++)        {                 // If requirements are met            if (array[i] <= count)            {                     // The Gi guest decides to stay                // So increment total guest by 1                count++;            }        }             // Return the totalnumber of guest        return count;    }         // Driver code    public static void Main ()    {             // Get the number of guests invited        int N = 5;             // Guests array stores        // the requirement by each guest        int [] guests = { 1, 0, 2, 1, 3 };             // Get the total number of guests present        int totalGuests = findGuest(guests, N);             Console.WriteLine(totalGuests);    }} // This code is contributed by ihritik
<?php// PHP program to get the// total number of guests at the party      // Function to find the totalGuestsfunction findGuest($array, $N){    // Total guest before the party are 0    $count = 0;     // Checking requirements for each guest    for ($i = 0; $i < $N; $i++)    {         // If requirements are met        if ($array[$i] <= $count)        {             // The Gi guest decides to stay            // So increment total guest by 1            $count++;        }    }     // Return the totalnumber of guest    return $count;}  // Driver code // Get the number of guests invited$N = 5; // Guests array stores// the requirement by each Guest$guests = array(1, 0, 2, 1, 3 ); // Get the total number of guests present$totalGuests = findGuest($guests, $N); echo $totalGuests; // This code is contributed by ihritik ?>
<script> // javascript program to get the// total number of guests at the party    // Function to find the totalGuests    function findGuest(array , N) {        // Total guest before the party are 0        var count = 0;         // Checking requirements for each guest        for (i = 0; i < N; i++) {             // If requirements are met            if (array[i] <= count) {                 // The Gi guest decides to stay                // So increment total guest by 1                count++;            }        }         // Return the totalnumber of guest        return count;    }     // Driver code              // Get the number of guests invited        var N = 5;         // Guests array stores        // the requirement by each guest        var guests = [ 1, 0, 2, 1, 3 ];         // Get the total number of guests present        var totalGuests = findGuest(guests, N);         document.write(totalGuests); // This code contributed by gauravrajput1 </script>
2
 
shrikanth13
Code_Mech
ihritik
GauravRajput1
Puzzles
School Programming
Puzzles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
Algorithm to solve Rubik's Cube
Puzzle 7 | (3 Bulbs and 3 Switches)
Top 20 Puzzles Commonly Asked During SDE Interviews
Puzzle 4 | (Pay an employee using a gold rod of 7 units ?)
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects