Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • GATE 2026 Prep
    • Get 3 IBM Certifications
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • Data Analytics Training
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation with DSA
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science & ML Program
    • All Courses
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • GATE
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Practice Coding Problems
    • GfG 160: Free DSA Practice
    • Problem of the Day
    • ETS TOEFL: Scholarship Contest
    • All Contests and Events
  • Jobs
    • Become a Mentor
    • Apply Now!
    • Post Jobs
    • Job-A-Thon: Hiring Challenge
  • Notifications

    Mark all as read
    All
    View All
    Notifications
    Mark all as read
    All
    Unread
    Read
    You're all caught up!!
  • Puzzles
  • Funny Riddles
  • Interesting Riddles
  • Mathematical Riddles
  • Animal Riddles
  • Mathematical Puzzles
  • Rubik's Cube
  • Aptitude-Puzzles
  • Top 100 Puzzles
  • Puzzles Quiz
Open In App
  • Explore GfG Courses
  • Share Your Experiences
  • Find number of candidates in the ExamPrint Maximum Meetings in One RoomFind the number of spectators standing in the stadium at time tMaximize the happiness of the groups on the TripFind Multiples of 2 or 3 or 5 less than or equal to N
  • DSA to Development Course

Find the total guests that are present at the party

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ program to get the // total number of guests at the party  #include <bits/stdc++.h> using namespace std;  // Function to find the totalGuests 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 int 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
// Java program to get the // total number of guests at the party 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(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
# Python3 program to get the # total number of guests at the party  # Function to find the totalGuests def 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 code N = 5 guests = [1, 0, 2, 1, 3] totalGusets = findGuest(guests, N) print(totalGusets)  # This code is contributed by Shrikant13 
C#
// C# program to get the // total number of guests at the party using 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 // PHP 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     $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  ?> 
JavaScript
<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> 

Output: 
2

 

Time complexity: O(N) where N is size of given input array

Auxiliary Space: O(1)


Next Article
Find the total guests that are present at the party

R

RuchaDeodhar
Improve
Article Tags :
  • Competitive Programming
  • C++ Programs
  • Programming Language
  • C++
  • Puzzles
  • DSA
Practice Tags :
  • CPP
  • Puzzles
course-img
631k+ interested Geeks
DSA to Development: A Complete Guide
course-img
470k+ interested Geeks
Complete Machine Learning & Data Science Program
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Careers
  • In Media
  • Contact Us
  • Corporate Solution
  • Campus Training Program
  • Explore
  • Job-A-Thon
  • Offline Classroom Program
  • DSA in JAVA/C++
  • Master System Design
  • Master CP
  • Videos
  • Tutorials
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • DSA Interview Questions
  • Competitive Programming
  • Data Science & ML
  • Data Science With Python
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • NodeJs
  • Bootstrap
  • Tailwind CSS
  • Python Tutorial
  • Python Examples
  • Django Tutorial
  • Python Projects
  • Python Tkinter
  • Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Computer Science
  • GATE CS Notes
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • DevOps
  • Git
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Databases
  • SQL
  • MYSQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • Preparation Corner
  • Company-Wise Recruitment Process
  • Aptitude Preparation
  • Puzzles
  • Company-Wise Preparation
  • More Tutorials
  • Software Development
  • Software Testing
  • Product Management
  • Project Management
  • Linux
  • Excel
  • All Cheat Sheets
  • Courses
  • IBM Certification Courses
  • DSA and Placements
  • Web Development
  • Data Science
  • Programming Languages
  • DevOps & Cloud
  • Programming Languages
  • C Programming with Data Structures
  • C++ Programming Course
  • Java Programming Course
  • Python Full Course
  • Clouds/Devops
  • DevOps Engineering
  • AWS Solutions Architect Certification
  • Salesforce Certified Administrator Course
  • GATE 2026
  • GATE CS Rank Booster
  • GATE DA Rank Booster
  • GATE CS & IT Course - 2026
  • GATE DA Course 2026
  • GATE Rank Predictor
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences